其他分享
首页 > 其他分享> > android-无法访问Context.USAGE_STATS_SERVICE

android-无法访问Context.USAGE_STATS_SERVICE

作者:互联网

我正在尝试通过以下方式访问lollipop中的Usage_Stats_Service:

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

我可以转到Context类,看看此常量如何存在,但Android Studio一直说这不是有效的常量.

我也尝试过使用原义字符串,但似乎getSystemService有一个约束,只能接受@ServiceName常量.

我已经花了将近2个小时的时间,却没有找出正在发生的事情.欢迎任何帮助.

解决方法:

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

它不是公开可用的常量,因为在source code中使用@hide注释隐藏了USAGE_STAT_SERVICE.documentation中缺少USAGE_STAT_SERVICE支持了这一点.

getSystemService has a constraint to only accept @ServiceName
constants.

这是由于“属性检查”引起的,不应影响编译.可以是turned off.

我还使用直接字符串成功获取了UsageManagerService的实例.

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

此外,值得注意的是permission required

android.permission.PACKAGE_USAGE_STATS

Studio只是将其标记为无法授予的权限(根据文档,技术上是正确的),因此Studio目前不知道诸如此类的“特殊”权限.

可以在以下位置授予使用权限

Settings > Security > Apps with usage access

标签:android-5-0-lollipop,android,service
来源: https://codeday.me/bug/20191028/1954941.html