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
----------------------- */
}
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;
}