How to get Usage Access Permission programmatically
<p>My app needs to have <strong>Usage Access Permission</strong> in order to get information about the current running app on user's phone. I am able to successfully implement it using the following code with the help from the following link.</p>

<p><a href="https://stackoverflow.com/questions/26431795/how-to-use-usagestatsmanager">Usage Access apps</a></p>

<p>Here is my working code </p>

<pre><code>public void showDialog()
 {
     if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.LOLLIPOP) {

         @SuppressWarnings("WrongConstant")
         UsageStatsManager usm = (UsageStatsManager) getSystemService("usagestats");
         long time = System.currentTimeMillis();
         List&lt;UsageStats&gt; 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();
     }
 }
</code></pre>

<p>This code will show a user an alert dialog that Usage Access permission is required and will take user to the settings screen from where it can be enabled.</p>

<p>Everything works perfectly with this. Only thing that I want is that <strong>how to give my app usage access permission by default without showing any dialog</strong> to user until he/she doesn't turn it off manually?</p>