title: How to check if "android.permission.PACKAGE_USAGE_STATS" permission is given?
body: <h2>Background</h2>
  
  <p>I'm trying to get app-launched statistics, and on Lollipop it's possible by using the <a href="https://developer.android.com/reference/android/app/usage/UsageStatsManager.html" rel="noreferrer"><strong>UsageStatsManager</strong></a> class, as such (original post <a href="https://stackoverflow.com/q/26431795/878126"><strong>here</strong></a>):</p>
  
  <p>manifest:</p>
  
  <pre><code>&lt;uses-permission
      android:name="android.permission.PACKAGE_USAGE_STATS"
      tools:ignore="ProtectedPermissions"/&gt;
  </code></pre>
  
  <p>opening the activity that will let the user confirm giving you this permission:</p>
  
  <pre><code>startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
  </code></pre>
  
  <p>getting the stats, aggregated :</p>
  
  <pre><code> private static final String USAGE_STATS_SERVICE ="usagestats"; // Context.USAGE_STATS_SERVICE);
   ...
   final UsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService(USAGE_STATS_SERVICE);
   final Map&lt;String,UsageStats&gt; queryUsageStats=usageStatsManager.queryAndAggregateUsageStats(fromTime,toTime);
  </code></pre>
  
  <h2>The problem</h2>
  
  <p>I can't seem to check if the permission that you need ("android.permission.PACKAGE_USAGE_STATS") is granted. All I've tried so far always returns that it is denied.</p>
  
  <p>The code works, but the permission check doesn't work well.</p>
  
  <h2>What I've tried</h2>
  
  <p>you can check for a permission being granted using either this:</p>
  
  <pre><code>String permission = "android.permission.PACKAGE_USAGE_STATS";
  boolean granted=getContext().checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;   
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>String permission = "android.permission.PACKAGE_USAGE_STATS";
  boolean granted=getPackageManager().checkPermission(permission,getPackageName())== PackageManager.PERMISSION_GRANTED;   
  </code></pre>
  
  <p>Both always returned that it got denied (even when I've granted the permission as a user).</p>
  
  <p>Looking at the code of UsageStatsManager, I've tried to come up with this workaround:</p>
  
  <pre><code>      UsageStatsManager usm=(UsageStatsManager)getSystemService("usagestats");
        Calendar calendar=Calendar.getInstance();
        long toTime=calendar.getTimeInMillis();
        calendar.add(Calendar.YEAR,-1);
        long fromTime=calendar.getTimeInMillis();
        final List&lt;UsageStats&gt; queryUsageStats=usm.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY,fromTime,toTime);
        boolean granted=queryUsageStats!=null&amp;&amp;queryUsageStats!=Collections.EMPTY_LIST;
  </code></pre>
  
  <p>It worked, but it's still a workaround.</p>
  
  <h2>The question</h2>
  
  <p>How come I don't get the correct result of the permission check?</p>
  
  <p>What should be done to check it better?</p>