我正在尝试获取手机上当前正在运行的应用程序列表。这是我正在使用的代码:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usm = (UsageStatsManager)this.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 10000*10000, time);
if (appList != null && appList.size() == 0) {
Log.d("Executed app", "######### NO APP FOUND ##########" );
}
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
Log.d("Executed app", "usage stats executed : " +usageStats.getPackageName() + "\t\t ID: ");
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
String currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
}
我在这里也设置了权限:
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
它没有检测到任何应用程序的使用。我在stackoverflow上尝试了一些其他方法,但似乎都没有效果。我甚至在某个地方读到过,在新款手机中,应用程序监视其他正在运行的应用程序的能力已完全删除。非常感谢任何帮助!
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
- SteelToerunningAppProcessInfo.get(i).processName
获取每个进程对应的应用程序包名字符串。 - SteelToe