Usage Access apps
这是我的工作代码
public voID showDialog() { if (androID.os.Build.VERSION.SDK_INT >= androID.os.Build.VERSION_CODES.LolliPOP) { @SuppressWarnings("WrongConstant") UsageStatsManager usm = (UsageStatsManager) getSystemService("usagestats"); long time = System.currentTimeMillis(); List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,time - 1000 * 1000,time); if (appList.size()==0) { AlertDialog alertDialog = new AlertDialog.Builder(this) .setTitle("Usage Access") .setMessage("App will not run without usage access permissions.") .setPositivebutton("Settings",new DialogInterface.OnClickListener() { @TargetAPI(Build.VERSION_CODES.LolliPOP) public voID onClick(DialogInterface dialog,int which) { // continue with delete Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); // intent.setComponent(new Componentname("com.androID.settings","com.androID.settings.Settings$SecuritySettingsActivity")); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivityForResult(intent,0); } }) .setNegativebutton("Cancel",new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog,int which) { // do nothing dialog.dismiss(); } }) .setIcon(androID.R.drawable.ic_dialog_alert) .create(); alertDialog.getwindow().setType(WindowManager.LayoutParams.TYPE_SYstem_ALERT); alertDialog.show(); } else { Intent intent = new Intent(this,PackageService.class); startService(intent); finish(); } }else { Intent intent = new Intent(this,PackageService.class); startService(intent); finish(); } }
此代码将向用户显示一个警告对话框,该对话框需要使用“使用权限”权限,并将用户带到可以启用它的设置屏幕.
一切都与此完美配合.我唯一想要的是默认情况下如何给予我的应用程序使用权限访问权限,而不会向用户显示任何对话框,直到他/她没有手动将其关闭?
解决方法 您只需在清单上提供此权限,而忽略此权限错误:<使用的许可
机器人:名字= “androID.permission.PACKAGE_USAGE_STATS”
工具:忽略= “ProtectedPermissions”/>
使用以下代码进行许可
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!isAccessGranted()) { Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); startActivity(intent); }}private boolean isAccessGranted() { try { PackageManager packageManager = getPackageManager(); ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackagename(),0); AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); int mode = 0; if (androID.os.Build.VERSION.SDK_INT > androID.os.Build.VERSION_CODES.KITKAT) { mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,applicationInfo.uID,applicationInfo.packagename); } return (mode == AppOpsManager.MODE_ALLOWED); } catch (PackageManager.nameNotFoundException e) { return false; } }总结
以上是内存溢出为你收集整理的android – 如何以编程方式获取用法访问权限全部内容,希望文章能够帮你解决android – 如何以编程方式获取用法访问权限所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)