Hello I must be tired as I just can't seem to make this work, in my mind it makes sense. I have a String[] full of Android app names that I am trying to filter out before I write the results to a JSONArray.
public static JSONArray appInfo = new JSONArray();
public static String[] ignoreList = {"Google", "Maps", "Settings", "Phone", "Chrome", "Photos", "Duo", "YouTube", "Gmail", "Contacts", "Drive", "Clock", "Messages", "Files", "Calculator", "File manager", "Samsung", "Gallery", "LastPass", "OnePlus", "Weather"};
public void listApps(Context c) {
PackageManager pm = c.getPackageManager();
Intent main = new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> packages = pm.queryIntentActivities(main, 0);
for (ResolveInfo resolve_info : packages) {
try {
String package_name = resolve_info.activityInfo.packageName;
String app = (String) pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));
for (String tempStr : ignoreList) {
if (!app.contains(tempStr)) {
JSONObject json = new JSONObject(); // Create temp json object with app info.
json.put("app", app);
appInfo.put(json);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Log.d("Debug", appInfo.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
If I was to check;
if (!app.contains("Google")) {
etc.. Then my JSONArray would contain no app names with "Google" but when I iterate over the ignoreList I get the following JSON out which is from the ignoreList not the remaining items that are not in the ignorList? Also it keeps repeating the objects? Any help would be very welcomed.
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Chrome"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Phone"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Settings"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Google Play Store"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Drive"
},
{
"app": "Maps"
},
{
"app": "Maps"
},
{
"app": "Maps"
},
{
"app": "Maps"
}