Notbad
Notbad

Reputation: 6296

No way to access Context.USAGE_STATS_SERVICE

I'm trying to access the Usage_Stats_Service in lollipop through this :

final UsageStatsManager usageStatsManager=(UsageStatsManager)this.getSystemService(Context.USAGE_STATS_SERVICE);

I can go to the Context class and see how this constant exist but Android Studio keeps saying that it is not a valid constant.

I have tried to use the literal string too but it seems getSystemService has a constraint to only accept @ServiceName constants.

I have spent almost 2 hours into this without finding out what is hapenning. Any help is welcomed.

Upvotes: 3

Views: 3062

Answers (1)

A--C
A--C

Reputation: 36449

Android Studio keeps saying that it is not a valid constant.

It isn't a publicly available constant because USAGE_STAT_SERVICE is hidden using the @hide annotation in the source code. This is supported by the lack of USAGE_STAT_SERVICE in the documentation.

getSystemService has a constraint to only accept @ServiceName constants.

This is due to "attribute inspection" and should not affect compilation. It can be turned off.

I've also successfully gotten an instance of UsageManagerService using a direct string.

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

Additionally, it is worth noting that the permission required,

android.permission.PACKAGE_USAGE_STATS

is simply flagged by Studio as a permission that can't be granted (technically true based on the documentation) so Studio at this time, does not know about "special" permissions such as these.

Usage Access can be granted in

Settings > Security > Apps with usage access

Upvotes: 7

Related Questions