问题描述
有容易得到从在Android L时ActivityManager服务正在运行的任务的列表,并且当前活动任务首先被返回。但是,不要在Android M编辑任何更多的,则返回列表仅包含我的应用程序的任务。有什么办法来解决呢?
我的code:
名单,其中,ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
的for(int i = 0; I< runningAppProcessInfos.size();我++){
ActivityManager.RunningAppProcessInfo runningAppProcessInfo = runningAppProcessInfos.get(ⅰ);
如果(runningAppProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
返回runningAppProcessInfo.pkgList [0];
}
}
您可以使用下面code和获取当前的前台活动的包名。
如果(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_ codeS.LOLLIPOP){
UsageStatsManager USM =(UsageStatsManager)getSystemService(usagestats);
很长一段时间= System.currentTimeMillis的();
名单< UsageStats> APPLIST = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
时间 - 1000 * 1000,时间);
如果(APPLIST =空&安培;!&安培; appList.size()大于0){
SortedMap的<长,UsageStats> mySortedMap =新TreeMap的<长,UsageStats>();
对于(UsageStats usageStats:APPLIST){
mySortedMap.put(usageStats.getLastTimeUsed(),
usageStats);
}
如果(mySortedMap =空&安培;!&安培;!mySortedMap.isEmpty()){
currentApp = mySortedMap.get(
。mySortedMap.lastKey())getPackageName();
}
}
} 其他 {
名单< ActivityManager.RunningAppProcessInfo>任务=上午
.getRunningAppProcesses();
currentApp = tasks.get(0).processName;
}
修改
添加此权限到清单文件。
<使用-权限的Android:名称=android.permission.GET_TASKS/>
<使用-权限的Android:名称=android.permission.PACKAGE_USAGE_STATS工具:忽略=ProtectedPermissions/>
注意
请确保您需要配置自定义您的设备来获得输出设置,您可以设置>安全性>应用程序与使用权限配置它>然后让你的应用程序的权限
It is easy to get a list of running tasks from the ActivityManager service on Android L, and the current active task is returned first. But it don't work on Android M any more, the return list only contains my app task. Is there any way to settle it?
My code:
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfos.size(); i++) {
ActivityManager.RunningAppProcessInfo runningAppProcessInfo = runningAppProcessInfos.get(i);
if (runningAppProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return runningAppProcessInfo.pkgList[0];
}
}
you can use below code and get the current foreground activity package name.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usm = (UsageStatsManager) 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();
}
}
} else {
List<ActivityManager.RunningAppProcessInfo> tasks = am
.getRunningAppProcesses();
currentApp = tasks.get(0).processName;
}
Edit
Add this permission in to Manifest file.
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />
Note
Make sure you need to configure custom setting in your device to obtain the output you can config it with Setting > Security > Apps with usage access > Then enable your app permission
这篇关于安卓M:我怎样才能获得当前的前台活动的包名(从服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!