我正在使用新的UsageStatsManager
API to 获取当前前台应用程序 https://stackoverflow.com/a/26885469/444324在 Android 5.0 棒棒糖中。
为了使用此 API,用户必须在Settings->Security->Apps with usage access
screen.
我使用以下 Intent 将用户直接发送到此屏幕:
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
现在,我想验证用户启用了我的应用程序。
我想这样做,就像我验证用户使我的应用程序能够使用NotificationListenerService
但我不知道 String 键是什么,如果它存在的话。
Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
// Tried Settings.ACTION_USAGE_ACCESS_SETTINGS as key but it returns null
在 Twitter 上收到某人的精彩回答,经过测试可以正常工作:
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
return (mode == AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)