1

I'm trying to develop an app that can get the Network stats from specific packages, but I'm getting these problems:

  1. When I try to use the NetworkStats Library of Android 6.0(Marshmallow), I get this Exception:

NetworkStats: Neither user 10412 nor current process has android.permission.READ_NETWORK_USAGE_HISTORY.

Here is the 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());
}
  1. How can I get the functionality of NetworkStats in previous versions of Android (5.0 & 4.0)? Is there any library?
Jehy
  • 4,729
  • 1
  • 38
  • 55
mizquierdo
  • 101
  • 5
  • 3
    "When I try to use the NetworkStats Library of Android 6.0(Marshmallow) I get this Exception" -- please provide a [mcve], including the code where you are trying to use `NetworkStats` and the complete stack trace of the crash. "In Previous versions of Android (5.0 & 4.0) How can I get the functionality of NetworkStats? Is there any library?" -- `TrafficStats` is as close as you will get, in terms of the Android SDK. Asking for off-site resources, like libraries, is considered to be off-topic here. – CommonsWare Aug 16 '16 at 14:26

2 Answers2

0

READ_NETWORK_USAGE_HISTORY permission is only granted for system applications. So, unless you are using a rooted phone, you won't be able to use it. The only way out is to use TrafficStats which is available since API level 8.

Jehy
  • 4,729
  • 1
  • 38
  • 55
0

Mmm ... After a little bit research over internet I give up on using this Android Native Library. I have managed to make it work, but its necessary that the user enables it through "User Data Access" option (Located under Settings App). Then I try to launch this app to the user so the user can enable by himself, but this doesnt not work on many devices. See this link:

Devices without "Apps using Usage Data" or android.settings.USAGE_ACCESS_SETTINGS intent

enter image description here

Community
  • 1
  • 1
mizquierdo
  • 101
  • 5
  • I had it also working on my samsung note 3, also with the "user data access" approach in combination with using the NetworkStatsManager classes (iterating over Buckets etc, you know) but suddenly (after a minor rom upgrade), it does not work anymore :-( My app is not listed in the "user data access" list anymore.. :-( – Aydin K. Aug 20 '16 at 18:37
  • What a shame that this library is not fully available in the way we need. In my developer journey I ended up using the TrafficStats Library (https://developer.android.com/reference/android/net/TrafficStats.html) since its available from API 8, so I had to set up an Alarm to constantly record this statistics (because these values represent data usage from the last boot). I didnt find any other reliable and supported way to do it. Mmm... – mizquierdo Aug 21 '16 at 20:42
  • Well I would also give up and change to another approach, but in my case, it worked flawlessly in the past and it should work in sdk>=23 according to the official docs.. That's what frustates me a little bit but also motivates for finding a solution. I raised a question in SO yesterday ( http://stackoverflow.com/questions/39067283/app-not-listed-in-apps-with-usage-access-activity ), maybe someone from the community has a helping idea. – Aydin K. Aug 22 '16 at 08:56
  • Great. Hope someone can help (Y)! – mizquierdo Aug 22 '16 at 15:15