Android Lollipop know if app as Usage Stats access
Asked Answered
S

4

7

Since Android Lollipop, we have now an API for accessing apps usage stats. However, your app must be granted those permissions by the user.

I know that for redirecting the user to those settings using Settings.ACTION_USAGE_ACCESS_SETTINGS.

Now, my question is how do you know the user has granted you those permissions so that you can stop redirecting him to the settings.

Thanks!

Skied answered 6/12, 2014 at 9:46 Comment(2)
#27215513Disoblige
Possible duplicate of Check if my application has usage access enabledPeerless
N
7

you can simply query usagestats with daily interval and end time the current time and if nothing is returned this means the user hasn’t granted permissions

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean doIHavePermission(){


    final UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
    final List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, 0,  System.currentTimeMillis());

    return !queryUsageStats.isEmpty();
}

Daily interval with start date 0 and end date the current time must at least return todays usage.So it will be empty only if permissions are not granted.

Nadenenader answered 6/12, 2014 at 18:46 Comment(4)
Thanks for the answer. That's actually what I am already doing, I was hoping for a cleaner way. Especially because these queries are a bit slow.Skied
Maybe you can limit the time range for example set start date to yesterday in order to return less statistics.But I see your point...Nadenenader
If you put a large time range it will take too long, if you put a short one, it can happen the user has not been using the phone... Anyway, if no one comes up with a better answer in the next days I'll just accept yours. ThanksSkied
This is also the way we do it. We persist a "was last able to get any usage stat data" timestamp, if this is > 0 then the privilege must exist. It appears Google has not published a way to get at this setting directly yet. If you look in the Android src code it appears there is one there, but it is not reachable in the SDK yet (not a public setting). FYI Also note that we have identified some "Android 5" devices that are entirely missing the app.usage features at all (LG G3 and Samsung S5 so far).Atencio
B
7

Check this answer: Tomik's answer

If you hurry, here's the solution ;)

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static boolean usageAccessGranted(Context context) {
       AppOpsManager appOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
       int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
       android.os.Process.myUid(), context.getPackageName());
       return mode == AppOpsManager.MODE_ALLOWED;
    }
Bloodfin answered 25/9, 2015 at 9:13 Comment(1)
In lollipop 5.1 it always returns true even when it is not enabled.Delindadelineate
C
2

I stumbled on the same problem. On Samsung S5 Lollipop usage stats did not work with the following code:

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

However usage stats actually exist. With the following code one can open the security settings:

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings$SecuritySettingsActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

An the scroll to the bottom and there is usage stats. I also inspeced logs and by pressing usage stats, you are directed to SubActivity which contains UsageStats fragment. I tried the following code:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.SubSettings"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

But got security exception. The problem is that they didnt mark SubActivity as exported, so as far as I know its not possible to directly start SubActivity (usage stats window). The only solution is to take user to Securiy settings and them tell him to manually go to usage stats view and enable app.

If someone finds better solution it would be great!

Chlortetracycline answered 19/4, 2015 at 15:53 Comment(3)
I know it's a bit late, but I managed to do it with Intent intent = new Intent(); intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$UsageAccessSettingsActivity")); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);Tiga
This is a good answer, but it's in the wrong question. Can you move it here? https://mcmap.net/q/89514/-android-content-activitynotfoundexception-no-activity-found-to-handle-intent-act-android-settings-usage_access_settings/238753Peerless
@EduardoHerzer, I tested your solution on Samsung Galaxy S5 SM-G900I running Android 5.0 G900IZTU1BOA1 firmware, and it didn't work for me. Do you remember which device and firmware you used? And can you confirm that Settings.ACTION_USAGE_ACCESS_SETTINGS didn't work?Peerless
U
1

See ActivityNotFoundException in Lollipop when trying to launch activity with intent android.settings.USAGE_ACCESS_SETTINGS for a better way, since with method we cannot discern whether there are simple no stats for the time interval

Ubangishari answered 19/2, 2015 at 15:37 Comment(2)
Can you post the relevant content from your link?Cathern
This won't tell you if the setting is set to true. This other thread is dealing with the issue that the some Android 5 builds are actually missing the entire app usage library (or at least the intent names).Atencio

© 2022 - 2024 — McMap. All rights reserved.