Is android.permission.PACKAGE_USAGE_STATS Only for

2019-08-10 22:07发布

问题:

I am accessing user stats in the following ways:

 UsageStatsManager userStatsMgr = (UsageStatsManager)getSystemService("usagestats"); 

 List<UsageStats> userStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, timeStamp - 1000*200, timeStamp)

Seems to work for:

 android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP

But I am not sure whether it will continue to work on newer SDK? I see some warning while using this permission, but those were not specific. So I am wondering what exactly these restrictions are?

 ActivityManager mgr =  (ActivityManager)ctx.getSystemService(Context.ACTIVITY_SERVICE);
                            List<ActivityManager.RunningAppProcessInfo>   
     processes = manager.getRunningAppProcesses();
                            app=processes.get(0).processName;

And also

      ActivityManager actMgr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                    actMgr.killBackgroundProcesses(process);

回答1:

android.permission.PACKAGE_USAGE_STATS can only be granted by a system activity, and the activity may not be present on all devices. Manufacturers that are known to have removed it from at least some of their devices include Samsung and LG.

The intent action to launch the system activity is Settings.ACTION_USAGE_ACCESS_SETTINGS. Check that Intent#resolveActivity(...) returns a non-null value before attempting to launch it.