title: NetworkStats in Android 6.0 - Permissions issues
body: <p>I'm trying to develop an app that can get the Network stats from specific packages, but I'm getting these problems:</p>
  
  <ol>
  <li>When I try to use the NetworkStats Library of Android 6.0(Marshmallow), I get this Exception:</li>
  </ol>
  
  <blockquote>
    <p>NetworkStats: Neither user 10412 nor current process has
    android.permission.READ_NETWORK_USAGE_HISTORY.</p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>try {
  
      TelephonyManager tm;String subscriberID;
      NetworkStatsManager networkStatsManager;
      NetworkStats networkStats;NetworkStats.Bucket bucket;
  
      tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
      subscriberID = tm.getSubscriberId();
      networkStatsManager = mContext.getSystemService(NetworkStatsManager.class);
      networkStats = networkStatsManager.queryDetailsForUid
              (typeMobile, subscriberID, dtBegin, dtEnd, uidPackage);
  
      if(networkStats !=null){
          while (networkStats.hasNextBucket()) {
              bucket = new NetworkStats.Bucket();
              networkStats.getNextBucket(bucket);
              Log.d("Bucket RX:",bucket.getUid()+" -" +String.valueOf(bucket.getRxBytes()));
              Log.d("Bucket TX:",bucket.getUid()+" -" +String.valueOf(bucket.getTxBytes()));
          }
      }
  }
  catch (Exception ex){
      Logger.e(ex.getMessage());
  }
  </code></pre>
  
  <ol start="2">
  <li>How can I get the functionality of NetworkStats in previous versions of Android (5.0 &amp; 4.0)? Is there any library?</li>
  </ol>