getRunningAppProcesses returns empty list on Android M(5.1.1)
I decide to use getRunningServices instead!
Du Speed Booster & Power Clean uses getRunningServices instead, maybe getRunningAppProcesses is deprecated in future.
Thank you google, thank you alphabet.
Hashtable<String, List<ActivityManager.RunningServiceInfo>> hashtable = new Hashtable<String, List<ActivityManager.RunningServiceInfo>>();
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo rsi : am.getRunningServices(Integer.MAX_VALUE)) {
if (isCanceled()) {
return;
}
String pkgName = rsi.service.getPackageName();
if (hashtable.get(pkgName) == null) {
List<ActivityManager.RunningServiceInfo> list = new ArrayList<ActivityManager.RunningServiceInfo>();
list.add(rsi);
hashtable.put(pkgName, list);
} else {
hashtable.get(pkgName).add(rsi);
}
}
int i = 0;
int size = hashtable.size();
for (Iterator it = hashtable.keySet().iterator(); it.hasNext(); i++) {
String key = (String) it.next();
List<ActivityManager.RunningServiceInfo> value = hashtable.get(key);
ProcessItem item = new ProcessItem(getContext(), value.get(0).pid, key, totalCpu, totalRam);
if (!whiteList.contains(item.pkgName)) {
if (!killList.contains(item.pkgName)) {
killList.add(item.pkgName);
ramTotal += item.ram;
if (getListener() != null) {
Progress progress = new Progress(this);
progress.setArg1(i);
progress.setArg2(size);
progress.setMsg(item.appName);
progress.setObj(item);
getListener().onExamining(progress);
}
}
}
}
hashtable.clear();
hashtable = null;
getRunningAppProcesses() returns only my app
i use this library
https://github.com/jaredrummler/AndroidProcesses/
and i update my code to this
list = new ArrayList<AppProcessInfo>();
ApplicationInfo appInfo = null;
AppProcessInfo abAppProcessInfo = null;
List<ActivityManager.RunningAppProcessInfo> appProcessList = activityManager
.getRunningAppProcesses();
// publishProgress(0, appProcessList.size());
List<AndroidAppProcess> processes = AndroidProcesses.getRunningAppProcesses();
for (AndroidAppProcess appProcessInfo : processes) {
String processName = appProcessInfo.name;
Stat stat = null;
try {
stat = appProcessInfo.stat();
} catch (IOException e) {
e.printStackTrace();
}
abAppProcessInfo = new AppProcessInfo(
appProcessInfo.name, stat.getPid(),
appProcessInfo.uid);
try {
appInfo = appProcessInfo.getPackageInfo(mContext, 0).applicationInfo;
.........................
it's working now
thank you
Android Java Get All Running Apps Even If They Are Running In Background
This looks a lot like Android 5.1.1 and above - getRunningAppProcesses() returns my application package only
...Android 5.1.1... killed getRunningAppProcesses()... It now returns a list of your own application package.
Looks like they nerfed the method you're relying on in the exact way you're seeing. I think you might have to find a different way to do what you're doing if you can, but doubt this one will work on modern versions. It also seems like a lot of threads talk about the app/play stores writing policies that bar apps from being approved that take advantage of these permissions, so if you plan to publish your app (for whatever solution you come up with), don't forget to check into that angle too.
check specific package name running in android foreground
Finally found the answer
Android 5.1.1 and above - getRunningAppProcesses() returns my application package only
UsageStatsManager usm = (UsageStatsManager)this.getSystemService("usagestats");
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
Related Topics
How to Change Datepicker Dialog Color for Android 5.0
How to Dismiss the Dialog with Click on Outside of the Dialog
Saving Edittext Content in Recyclerview
How to Simulate a Touch Event in Android
How to Programmatically Move, Copy and Delete Files and Directories on Sd
How to Save/Store Objects in Sharedpreferences on Android
How to Parse the Androidmanifest.Xml File Inside an .Apk Package
Sending Message Through Whatsapp
Tileprovider Using Local Tiles
How to Use Scrollview in Android
Integrate Mupdf Reader in an App
How to Solve Java.Lang.Outofmemoryerror Trouble in Android
Receiving Package Install and Uninstall Events
How to Have Onscrolllistener for a Scrollview
Trying to Attach a File from Sd Card to Email
Resume Application and Stack from Notification