1

I had tested this solution to list all running apps and always is be repeating the same list (even if device is rebooted). Seems that exist a history that must be erased before each execution of this code to obtain a new list of apps that is actually running.

Then i want know if exist some solution that is able to erase this supposed history?

In negative case, what other way that can solve this trouble?

public static String getActiveApps(Context context) {

    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    String value = u.dateStamp(); // basic date stamp
    value += "---------------------------------\n";
    value += "Active Apps\n";
    value += "=================================\n";

    for (ApplicationInfo packageInfo : packages) {

        //system apps! get out
        if (!isSTOPPED(packageInfo) && !isSYSTEM(packageInfo)) {

            value += getApplicationLabel(context, packageInfo.packageName) + "\n" + packageInfo.packageName  + "\n-----------------------\n";

        }
    }

    return value;

    //result on my emulator

    /* 2 Ekim 2017 Pazartesi 14:35:17
    ---------------------------------
    Active Apps
    =================================
    SystemSetting
    com.xyz.systemsetting
    -----------------------
    myMail
    com.my.mail
    -----------------------
    X-plore
    com.lonelycatgames.Xplore
    -----------------------
    Renotify
    com.liamlang.renotify
    -----------------------
    Mail Box
    com.mailbox.email
    -----------------------   */


}

some opened apps

isSTOPPED

private static boolean isSTOPPED(ApplicationInfo pkgInfo) {

    return ((pkgInfo.flags & ApplicationInfo.FLAG_STOPPED) != 0);
}

isSYSTEM

private static boolean isSYSTEM(ApplicationInfo pkgInfo) {

    return ((pkgInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

getApplicationLabel

public static String getApplicationLabel(Context context, String packageName) {

    PackageManager        packageManager = context.getPackageManager();
    List<ApplicationInfo> packages       = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
    String                label          = null;

    for (int i = 0; i < packages.size(); i++) {

        ApplicationInfo temp = packages.get(i);

        if (temp.packageName.equals(packageName))
            label = packageManager.getApplicationLabel(temp).toString();
    }

    return label;
}
  • please provide code – Enamul Haque Jan 21 '18 at 15:56
  • @EnamulHaque, the question was edited. –  Jan 21 '18 at 16:27
  • Your code has nothing to do with "list all running apps". It lists all *installed* applications. On modern versions of Android, you do not have a way to determine what the running apps are (for most definitions of "running"), for privacy and security reasons. – CommonsWare Jan 21 '18 at 16:29
  • @CommonsWare, Yes but he also check if some between the installeds is running or no. The trouble here, is that this list not is updated when my app is closed or device is rebooted for example. Tested with Android 5.1.1 and 7.1.1 –  Jan 21 '18 at 17:07
  • "Yes but he also check if some between the installeds is running or no" -- not in the code that is in your question. – CommonsWare Jan 21 '18 at 17:08
  • @CommonsWare, see => *he also check if some between the installeds is running or no* –  Jan 21 '18 at 17:10
  • @BrowJr: Not in the code that is your question. The code in your question filers out apps that have `FLAG_SYSTEM` or `FLAG_STOPPED`. My guess is that you think that `FLAG_STOPPED` has something to do with "is running or no". It does not. It has to do with whether the app is in the "stopped state" (i.e., has been installed but not launched or has been force-stopped from Settings). – CommonsWare Jan 21 '18 at 17:14
  • @CommonsWare, you is right about this: *`My guess is that you think that FLAG_STOPPED has something to do with "is running or no".`* Then when user press "X" on app window the app still continue running? –  Jan 21 '18 at 17:23
  • That removes the task and usually terminates the process. The process may get restarted shortly, depending on what services might be in use by that process. – CommonsWare Jan 21 '18 at 17:40
  • @CommonsWare, then this is a serious bug of Android, eg: on Windows system when click "**X**" on application window all is stopped and close :-). Android also could be this way. –  Jan 21 '18 at 17:52

0 Answers0