title: How to use UsageStatsManager?
body: <h2>Background</h2>
  
  <p>Google has deprecated the function "<a href="http://developer.android.com/reference/android/app/ActivityManager.html#getRecentTasks(int,%20int)" rel="noreferrer"><strong><code>getRecentTasks</code></strong></a>" of "ActivityManager" class. Now all it does is to get the list of apps that the current app has opened.</p>
  
  <p>I've even written a post about it <a href="https://stackoverflow.com/questions/24590533/how-to-get-recent-tasks-on-android-l"><strong>here on StackOverflow</strong></a>, but I noticed it's impossible.</p>
  
  <h2>The problem</h2>
  
  <p>I've made a post about it (<a href="https://code.google.com/p/android-developer-preview/issues/detail?id=507" rel="noreferrer"><strong>here</strong></a>, and another, similar one created by someone else, <a href="https://code.google.com/p/android-developer-preview/issues/detail?id=29" rel="noreferrer"><strong>here</strong></a>) and requested to re-consider it, and Google decided to make a new class, that seem to provide a similar functionality (more like statistics, but might also be useful), but I can't find out how to use it.</p>
  
  <p>The class is called "<code>UsageStatsManager</code>", and my guess is that the function "<a href="http://developer.android.com/reference/android/app/usage/UsageStatsManager.html#queryUsageStats(int,%20long,%20long)" rel="noreferrer"><strong><code>queryUsageStats</code></strong></a>" does the job.</p>
  
  <p>Also, it seems it has a new permission ("<code>android.permission.PACKAGE_USAGE_STATS</code>"), which is a <em>system permission</em>, but it's written that:</p>
  
  <blockquote>
    <p>declaring the permission implies intention to use the API and the user
    of the device can grant permission through the Settings application.</p>
  </blockquote>
  
  <p><a href="https://developer.android.com/about/versions/android-5.0.html#System" rel="noreferrer"><strong>Here's</strong></a> another link about this new functionality.</p>
  
  <h2>What I've found</h2>
  
  <p>I've looked at the code of Android, and noticed that "Context" has USAGE_STATS_SERVICE , which in the JavaDocs say the next thing:</p>
  
  <pre><code>/**
   * Use with {@link #getSystemService} to retrieve a {@link
   * android.app.UsageStatsManager} for interacting with the status bar.
   *
   * @see #getSystemService
   * @see android.app.UsageStatsManager
   * @hide
   */
  public static final String USAGE_STATS_SERVICE = "usagestats";
  </code></pre>
  
  <p>The weird thing is that not only it says "<code>status bar</code>", but also the packageName doesn't match (should be "<code>android.app.usage.UsageStatsManager</code>" instead) .</p>
  
  <p>I've also added the correct permission:</p>
  
  <pre><code>&lt;uses-permission
      android:name="android.permission.PACKAGE_USAGE_STATS"
      tools:ignore="ProtectedPermissions" /&gt;
  </code></pre>
  
  <p>and here's the code I use:</p>
  
  <pre><code>  final UsageStatsManager usageStatsManager=(UsageStatsManager)context.getSystemService("usagestats");// Context.USAGE_STATS_SERVICE);
    final int currentYear=Calendar.getInstance().get(Calendar.YEAR);
    final List&lt;UsageStats&gt; queryUsageStats=usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY,currentYear-2,currentYear);
  </code></pre>
  
  <p>In the emulator itself, I went to "<strong><em><code>Settings"-&gt;"security"-&gt;"apps with usage access</code></em></strong>" , and enabled my app.</p>
  
  <p><strong><em>However, when running the code, all I get is an empty list...</em></strong></p>
  
  <h2>The question</h2>
  
  <p>How do you use <code>UsageStatsManager</code> ? </p>
  
  <p>Also, how do you let the user to grant the permission in the easiest way possible? Or is it automatically done, as soon as the app tries to get the needed information?</p>
  
  <p>What happens when trying to use this class yet the user hasn't confirmed it yet?</p>
  
  <p>How can I make the code return me a real list of apps?</p>