Solution 1 :
Now you have two issues:
-
Repetition issue: because you don’t break the for loop whenever you add your
app
to the JSON list, so you continue iterating
theignoreList
and add more instances of theapp
to the JSON
list. to avoid that you need to add abreak
statement. -
Adding apps that should be ignored: this is because you are adding the
app
to the JSON list before comparing every
instance from theignoreList
theapp
.. it looks vague and also
to me 🙂 so I will explain with example.
Example:
Consider your app = “Settings“, and now you are comparing it to the list using the for loop, so first you compare it to “Google“, it doesn’t equal it, so add it to the JSON list
, the same applies when you compare it to “Maps“, and so on until you compare it to “Settings“, at this time it won’t be added to the JSON list
, but you already added it multiple times before you reach to “Settings”, so you need to wait until you finish your entire iterations before adding the app
to the JSON list
. I used a boolean
to achieve that.
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));
boolean isIgnored = false;
for (String tempStr : ignoreList) {
if (app.toLowerCase().contains(tempStr.toLowerCase())) {
isIgnored = true;
break; // to avoid unneeded iterations
}
}
if (!isIgnored) {
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();
}
}
Problem :
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"
}
Comments
Comment posted by D4rkC00d3r
Thank you so much for the detailed response and taking the time to answer, it makes perfect sense.