【发布时间】:2018-09-19 10:59:18
【问题描述】:
我正在尝试获取我手机上当前正在运行的应用程序的列表。这是我正在使用的代码:
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(); -
然后
runningAppProcessInfo.get(i).processName得到一个字符串,代表每个进程的应用包名 -
@SteelToe 我有。这只给了我自己的应用程序和谷歌搜索栏,而手机上运行着很多其他应用程序。
-
您在尝试您的代码时是否确保获得了适当的权限?
标签: android android-permissions usagestatsmanager