3

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);
Md. Sabbir Ahmed
  • 850
  • 8
  • 22
MuayThai
  • 441
  • 1
  • 5
  • 19

1 Answers1

5

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.

Community
  • 1
  • 1
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • I am able to use the PACKAGE_USAGE_STATS on all devices I have tried so far.. Lots of devices that don't have Settings.ACTION_USAGE_SETTINGS still support this in fact its the older 2.3.6 devices in which I have seen Settings.ACTION_USAGE_SETTINGS removed. So not sure why reaching settings is related here? – MuayThai Sep 28 '15 at 09:34
  • @MuayThai It wasn't "removed" from older devices. The Usage Stats API did not exist before Android 5.0. And AFAIK, the activity launched by `Settings.ACTION_USAGE_ACCESS SETTINGS` is the only way to grant `android.permission.PACKAGE_USAGE_STATS` on any device. – Kevin Krumwiede Sep 28 '15 at 16:40