14

我的应用程序需要具有使用访问权限才能获取有关用户手机上当前正在运行的应用程序的信息。在以下链接的帮助下,我能够使用以下代码成功实现它。

使用访问应用程序

这是我的工作代码

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();
     }
 }

此代码将向用户显示需要使用访问权限的警报对话框,并将用户带到可以启用它的设置屏幕。

一切都与此完美配合。我唯一想要的是默认情况下如何授予我的应用程序使用权限而不向用户显示任何对话框,直到他/她没有手动关闭它?

4

1 回答 1

26

您可以简单地在清单上提供此权限,而忽略此权限错误:

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions"/>

比使用以下代码获得许可

@Override
protected 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;
        }
    }
于 2017-02-10T07:13:51.223 回答