我刚刚测试了我的应用程序和CM,ATM Android助手等等。所有这些都无法获得正在运行的进程列表,但它们在预操作系统版本上运行良好。那么Android M(5.1.1)正在发生什么?请帮忙!
am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
Log.i(TAG, "LQ::examine list.size()=" + list.size());
答案 0 :(得分:6)
我决定改用getRunningServices!
Du Speed Booster&amp; Power Clean使用getRunningServices代替,也许将来不推荐使用getRunningAppProcesses。
谢谢谷歌,谢谢你的字母。
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;
答案 1 :(得分:1)
另一种选择是使用UsageStatsManager
。
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(Context.USAGE_STATS_SERVICE);
long endTime = System.currentTimeMillis();
long beginTime = endTime - 1000*60;
// We get usage stats for the last minute
List<UsageStats > stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, beginTime, endTime);
// Sort the stats by the last time used
if(stats != null)
{
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats)
{
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
}
if(mySortedMap != null && !mySortedMap.isEmpty())
{
topActivity = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
要使其正常工作,您需要PACKAGE_USAGE_STATS
权限。您可以通过在设置中打开屏幕来提示用户执行此操作:
Intent usageAccessIntent = new Intent( Settings.ACTION_USAGE_ACCESS_SETTINGS );
usageAccessIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( usageAccessIntent );
答案 2 :(得分:1)
我可以在android 6.0上使用$ python -c"import psycopg2 ; print('psycopg2 is now ok')"
)方法获取前台应用列表。谢谢@ thecr0w
getRunningServices(