How to get permission to use software

My application must have Resource Access Permission in order to get information about the currently running application on the user's phone. I can successfully implement it using the following code using the following link.

Apps to access apps

Here is my working code

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

This code will show the user a warning dialog about the need to use access permission and will lead the user to the settings screen, from where he can be included.

Everything works fine with this. The only thing I want is that how to provide access to access to use the default application without displaying any dialogue for the user until he / she disconnects it manually?

+7
source share
1 answer

You can simply grant this manifest permission, ignoring only this permission error:

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

How to use the following code to resolve

 @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; } } 
+14
source

All Articles