3

I'm trying to get statistics of traffic used on last 30days on each application using NetworkStatsManager , so i wrote a code as following :

Thread thread = new Thread(new Runnable() {
@Override
public void run() {

NetworkStatsManager networkStatsManager = (NetworkStatsManager) getActivity().getSystemService(Context.NETWORK_STATS_SERVICE);
    NetworkStats networkStats = null;
    Log.d("CCC","start----"+System.currentTimeMillis());
    networkStats = networkStatsManager.querySummary(ConnectivityManager.TYPE_WIFI,"",
                                System.currentTimeMillis()-2592000000L,
                                System.currentTimeMillis());
    Log.d("CCC","END-"+System.currentTimeMillis());
    } catch (RemoteException e) {}

    NetworkStats.Bucket bucket = new NetworkStats.Bucket();
    while (networkStats!=null && networkStats.hasNextBucket()) {
    networkStats.getNextBucket(bucket);  
    Log.d("CCC",bucket.getUid()+"---"+bucket.getTxBytes()+"---"+bucket.getRxBytes());
}
});
thread.start();

Returned result seems to be correct but the problem is, query takes too much time to respond -usually 25 seconds- and if i want to get both TYPE_WIFI and TYPE_MOBILE internet usage it will be over 50 seconds, which is not something good. So is there another approach to do this? Or perhaps i am doing something wrong here that causes this delay? Thank you in advance.

Mehrdad
  • 180
  • 1
  • 13
  • Apparently this only happends when you use NetworkStatsManager inside a thread . if you run the code on UIThread it would respond in about -500ms- which is very good but still makes the ui non-smooth. – Mehrdad Aug 20 '17 at 23:15

0 Answers0