title: How to check if a specific app has admin rights, and go to its admin screen?
body: <h2>Background</h2>
  
  <p>Android apps can gain some special rights using admin policies, like turning the lock screen on.</p>
  
  <p>As such, those apps cannot be uninstalled till the user has revoked their admin rights.</p>
  
  <p>I wish to have a function that will check if a given app (given a package name of the app) has admin rights, and if so, i will open the admin settings page of this app, to allow the user to revoke its rights.</p>
  
  <h2>The problem</h2>
  
  <p>As it turns out, there is already a function that does it.</p>
  
  <p>First you get an instance of the DevicePolicyManager :</p>
  
  <pre><code>DevicePolicyManager dpm=(DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
  </code></pre>
  
  <p>Then you are supposed to call <a href="http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#isAdminActive%28android.content.ComponentName%29" rel="noreferrer"><strong>isAdminActive</strong></a> , and get a result of true/false.</p>
  
  <p>However, this function's parameter is ComponentName, meaning it needs not only the package name, but also the path to a specific class of the app.</p>
  
  <p>An alternative would be to query all active admin apps, using <a href="http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#getActiveAdmins%28%29" rel="noreferrer"><strong>getActiveAdmins</strong></a>.</p>
  
  <p>Later, I you can go to the admin page using:</p>
  
  <pre><code>final Intent intent=new Intent();
  intent.setComponent(new ComponentName("com.android.settings","com.android.settings.DeviceAdminSettings"));
  startActivity(intent);
  </code></pre>
  
  <p>or even more specifically, to enable/disable the app admin rights :</p>
  
  <pre><code>final Intent intent=new Intent();
  intent.setComponent(new ComponentName("com.android.settings","com.android.settings.DeviceAdminAdd"));
  intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,componentName);
  activity.startActivity(intent);
  </code></pre>
  
  <p>However, both of those ways seems a bit hacky, as they do not use the official API of android, that could change over android versions and roms.</p>
  
  <h2>The question</h2>
  
  <p>What's the best way to check if a given app (package name) has admin rights?</p>
  
  <p>What's the best way to go to the admin page ? is there a safer way to go there? maybe a fallback mechanism from what i've done to the security screen intent (which for some reason I can't find in the API) ?</p>
  
  <hr>
  
  <h2>The answer</h2>
  
  <p>After reading the answer on this post, I've decided to share the findings:</p>
  
  <p>If you wish to be safe, you can tell the user to go to the admin screen by himself, and only guide him to the security screen:</p>
  
  <pre><code>intent=new Intent(Settings.ACTION_SECURITY_SETTINGS);
  context.startActivity(intent);
  </code></pre>
  
  <p>however, i think the next way should work for all devices:</p>
  
  <pre><code>Intent intent=new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  final PackageManager packageManager=context.getPackageManager();
  final List&lt;ResolveInfo&gt; resolveInfos=packageManager.queryIntentActivities(intent,0);
  if(resolveInfos!=null&amp;&amp;!resolveInfos.isEmpty())
    try
      {
      final ResolveInfo resolveInfo=resolveInfos.get(0);
      intent=new Intent();
      intent.setComponent(new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name));
      intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,componentNameResult);
      context.startActivity(intent);
      return true;
      }
    catch(final Exception e)
      {}
  </code></pre>
  
  <p>and if it fails, you can always use any of the methods i've written, and in the end, call the safe one.</p>