我正在使用来自Android6 (API 23)上的isAppInactive()类的UsageStatsManager方法来检测我正在开发的应用程序的备用状态。
文档确认请求PACKAGE_USAGE_STATS权限是必要的,但我可以在不请求权限的情况下使用该方法。
这是个虫子吗?
发布于 2015-11-11 16:27:33
在UsageStatsService中,有一个hasPermission [1]方法,它检查权限PACKAGE_USAGE_STATS是否被授予。在这种方法中使用的是:
queryEvents [2]queryConfigurations [3]queryUsageStats [4]queryAndAggregateUsageStats (它使用相同的queryUsageStats方法)这是isAppInactive [5]的代码,您可以看到如何不请求权限:
@Override
public boolean isAppInactive(String packageName, int userId) {
try {
userId = ActivityManagerNative.getDefault().handleIncomingUser(Binder.getCallingPid(),
Binder.getCallingUid(), userId, false, true, "isAppInactive", null);
} catch (RemoteException re) {
return false;
}
final long token = Binder.clearCallingIdentity();
try {
return UsageStatsService.this.isAppIdleFilteredOrParoled(packageName, userId, -1);
} finally {
Binder.restoreCallingIdentity(token);
}
}正如您在提交的消息中看到的那样,它添加了isAppInactive (最初称为isAppIdle,然后被重命名),API应该是公共的:
Add ability to get and set idle state of apps
Add am shell command to set and get idle
Add public API to check if an app is idle我不认为这是一个bug,但只是一个不清楚的文档。
https://stackoverflow.com/questions/33654694
复制相似问题