Cannot use getSystemService(“usagestats”) Android Studio

来源:互联网 发布:ipad能用mac系统 编辑:程序博客网 时间:2024/05/21 12:50


8down votefavorite
4

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?

shareimprove this question
 

1 Answer

activeoldestvotes
up vote7down vote

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

//noinspection ResourceTypeUsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService("usagestats");
shareimprove this answer
 
 
works like a charm! how do u know that? – jiashie Dec 1 '15 at 8:34

这是我在stackoverflow上面找到的,意思是这个问题是android studio的bug  只需要在报错的这行上面加上//noinspection ResourceType  就可以解决了


8down votefavorite
4

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?

shareimprove this question
 

1 Answer

activeoldestvotes
up vote7down vote

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

//noinspection ResourceTypeUsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService("usagestats");
shareimprove this answer
 
   
works like a charm! how do u know that? – jiashie Dec 1 '15 at 8:34
原创粉丝点击