Android 有权查看使用权限的应用
Android About 2,054 words有权查看使用权限的应用
Android5.0
版本后获取栈顶应用的方法getRunningTask
方法被Google
给屏蔽掉了。
使用UsageStatsManage
r获取,但是这种获取方法需要用户在手机上赋予APP
权限才可以使用,就是在安全-高级-有权查看使用情况的应用。
- 权限
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
- 判断当前设备中有没有“有权查看使用权限的应用”这个选项
private boolean hasUsageAccessSettingsOption() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
} else {
return false;
}
}
- 判断调用该设备中“有权查看使用权限的应用”这个选项的
APP
有没有打开
private boolean isUsageStatsServiceOpen() {
List<UsageStats> queryUsageStats = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, 0, System.currentTimeMillis());
}
if (queryUsageStats == null || queryUsageStats.isEmpty()) {
return false;
}
return true;
}
startActivityForResult
打开“有权查看使用情况的应用”页面
private void startUsageAccessSettingsActivity() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivityForResult(intent,100);
}
}
onActivityResult
再作判断
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
boolean serviceOpen = isUsageStatsServiceOpen();
if (!serviceOpen) {
Toast.makeText(getApplicationContext(), "请在设置界面开启", Toast.LENGTH_SHORT).show();
} else {
//TODO 已打开
}
}
}
Views: 7,256 · Posted: 2019-04-13
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...