Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – JAVA check if a string does not contain a sub string from a string array,

Posted on November 14, 2022

Solution 1 :

Now you have two issues:

  1. Repetition issue: because you don’t break the for loop whenever you add your app to the JSON list, so you continue iterating
    the ignoreList and add more instances of the app to the JSON
    list. to avoid that you need to add a break statement.

  2. Adding apps that should be ignored: this is because you are adding the app to the JSON list before comparing every
    instance from the ignoreList the app.. 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.

READ  [ANSWERED] java - Issue with CRC16, UShort in Kotlin
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress