13

I am trying to use UsageStatsManager with reference to this SO question. My line of code is

  UsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService("usagestats");

I also tried to use

  UsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService(Context.USAGE_STATS_SERVICE);

Regardless of what I use Android Studio says, "Must be one of: Context.POWER_SERVICE, Context.WINDOW_SERVICE..."

After a lot of research I came to know that Context.USAGE_STATS_SERVICE is hidden, so I must add the permission

<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions" />

But still, the results are same. What am I missing?

Community
  • 1
  • 1
Abhishek
  • 2,959
  • 2
  • 17
  • 24

1 Answers1

13

Must be some kind of bug in Android Studio. You can disable inspection by adding:

//noinspection ResourceType
UsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService("usagestats");
myfknoll
  • 455
  • 5
  • 15
  • 2
    It is not a bug in AS, `Context` wants it to be one of the constants annotated with `@ServiceName`. So it is a bug in Context. – Gokhan Arik Sep 12 '17 at 17:25