I'm trying to use NetworkStatsManager class to retrieve the data usage of my app. I want to track it once in an hour (intervals of an hour) and as I read queryDetails is not an option for me because it's just for 2-3 hours.
when I tried to use querySummary:
private HashMap<String, Long> getNetworkStats(NetworkStatsManager networkStatsManager,
int connectivityManager,
@Nullable String subscriberId,
long startTime,
long endTime) {
long foreground = 0;
long background = 0;
HashMap map = new HashMap<String, Long>();
try {
final NetworkStats networkStats =
networkStatsManager.querySummary(connectivityManager, subscriberId, startTime, endTime);
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
while (networkStats.hasNextBucket()) {
networkStats.getNextBucket(bucket);
if (bucket.getState() == NetworkStats.Bucket.STATE_FOREGROUND) {
foreground += bucket.getRxBytes() + bucket.getTxBytes();
} else {
background += bucket.getRxBytes() + bucket.getTxBytes();
}
}
} catch (RemoteException e) { }
map.put("foreground", foreground);
map.put("background", background);
return map;
}
Call to getNetworkStats with two different startTime:
long endTime = System.currentTimeMillis();
long startTime1 = endTime - (3600 * 1000); //One hour ago.
long startTime2 = endTime - (3600 * 1000 * 2); //Two hours ago.
getNetworkStats(networkStatsManager, ConnectivityManager.TYPE_WIFI, startTime1, endTime)
getNetworkStats(networkStatsManager, ConnectivityManager.TYPE_WIFI, startTime2, endTime)
return the same data. Why?