概要:
android L前我们可以使用
getRunningTasks(int maxNum)
maxNum int: The maxNumnumber of entries to return in the list. The actual number returned may be smaller, depending on how many tasks the user has started.
通过PackageManager.getApplicationInfo(pakgName, 0)来获取对应包名的应用信息,而在lolipop之后,google在android 5.0 系统中收紧了对API的使用权限,导致我们在使用上边两个方法的时候,有可能只会获取到我们自身应用的包名。针对这一情况,网上也出现了很多的解决方案,今天我们就来了解一二。
获取进程方法:
首先还是来介绍下上边提到的两种方法:
1. getRunningTasks(int maxNum)
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 1是返回应用列表的size,0是获取当前列表第一个RunningTaskInfo
// RunningTaskInfo.topActivity:The activity component at the top of the history stack of the task.
ComponentName cn = activityManager.getRunningTasks(1).get(0).topActivity;
String currentPkgName = cn.getPackageName(); // 当前显示在顶端的包名
当然熟悉的朋友应该了解这里需要添加一个权限
This method was deprecated in API level 21. As of LOLLIPOP
this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks , and possibly some other tasks such as home that are known to not be sensitive.
从官方文档可以看到这个方法在5.0之后已经deprecate了。
2. getRunningAppProcesses()
ActivityManager activityManager = (ActivityManager)context.getApplicationContext().getSystemService( Context.ACTIVITY_SERVICE);
List processInfos = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
// 如果有需要这里可以遍历当前系统所有的进程
// String pkgName = processInfo.processName;
// ApplicationInfo applicationInfo = PackageManager.getApplicationInfo(pkgName, 0);
// 这里开始获取顶端的进程信息
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
Log.i("top_info", "当前顶端进程的信息");
Log.i("process_pid", "进程id: " + processInfo.pid);
Log.i("proccess_name", "进程名 : " + processInfo.processName);
Log.i("process_pkgs", "该进程下可能存在的包名");
for (String pkgName : processInfo.pkgList) {
Log.i("pkg_names", " " + pkgName);
}
return processInfo.pkgList;
}
}
Returns a list of application processes that are running on the device.
不过在L上获取process还是有一些问题
3. UsageStatsManager
UsageStatsManager是在5.0之后google提供给我们的一个新的API。不过同样的需要用户提供权限。需要我们在获取进程之前,询问是否用户允许了该权限。
首先添加manifest权限
tools:ignore="ProtectedPermissions" />
顺带可以看下怎样引导用户开通对应的权限的问题:
判断是否开启了权限
@SuppressLint("NewApi")
public static boolean hasEnable(Context context){
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ // 如果大于等于5.0 再做判断
long ts = System.currentTimeMillis();
UsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService(Service.USAGE_STATS_SERVICE);
List queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, 0, ts);
if (queryUsageStats == null || queryUsageStats.isEmpty()) {
return false;
}
}
return true;
}
没有开启权限的跳转
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setComponent( new ComponentName("com.android.settings", "com.android.settings.Settings$SecuritySettingsActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
顺带提一下,有部分国产厂家的手机系统,在系统安全设置里边是没有有权查看使用情况的应用程序这个选项的。需要注