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.