I'm using the NetworkStatsManager class to retrieve the data usage of the apps.
If I provide 01.01.2018 as start time, it shows that my phone has sent out 5,4 GB.
If I provide 01.01.2017, it still shows 5,4 GB.
This lets me assume that there is a limit somehow regarding the start time. The documentation unfortunately does not mention anything regarding this.
So, how much in time can we go back?
Code:
This is the code which retrieves the data using querySummary:
private long[] getBytesSummary(Context context, int networkType, Calendar calendar) {
NetworkStatsManager networkStatsManager = (NetworkStatsManager) context.getSystemService(Context.NETWORK_STATS_SERVICE);
NetworkStats networkStats = null;
try {
networkStats = networkStatsManager.querySummary(
networkType,
Util.getSubscriberId(context, networkType),
calendar.getTimeInMillis(),
System.currentTimeMillis());
} catch (RemoteException e) {
if (debug) Log.e(TAG, "getBytesSummary: " + e.toString());
}
long[] result = new long[2];
long totalRxBytes = 0;
long totalTxBytes = 0;
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
if (networkStats != null) {
while (networkStats.hasNextBucket()) {
networkStats.getNextBucket(bucket);
int uid = bucket.getUid();
long uidRxBytes = bucket.getRxBytes();
long uidTxBytes = bucket.getTxBytes();
if (uidsWithNetworkUsageMap.indexOfKey(uid) < 0) {
long[] uidBytes = new long[2];
uidBytes[0] = uidRxBytes;
uidBytes[1] = uidTxBytes;
uidsWithNetworkUsageMap.put(uid, uidBytes);
} else {
long[] value = uidsWithNetworkUsageMap.get(uid);
value[0] = value[0] + uidRxBytes;
value[1] = value[1] + uidTxBytes;
uidsWithNetworkUsageMap.put(uid, value);
}
totalRxBytes += bucket.getRxBytes();
totalTxBytes += bucket.getTxBytes();
}
networkStats.close();
}
result[0] = totalRxBytes;
result[1] = totalTxBytes;
return result;
}
As you can see above, I give a calendar object to the method.
This is how I get the calendar for the current year:
public static Calendar getCalendarCurrentYear() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, 1);
// for 0-12 clocks
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
// for 0-24 clocks
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// calendar.set(Calendar.YEAR, 2017);
Log.i("DataUsage", "getCalendarCurrentYear: " + calendar.getTime());
return calendar;
}
This returns Mon Jan 01 00:00:00 GMT+01:00 2018.
If I set it now to 2017, the result is the same as for 2018.
Am I doing something wrong?
EDIT:
I just did some testing, the limit seems to be 3 months somehow.
Anything greater than 3 months as start time results in the same value.
Can anyone confirm that managed to get it working for more than 3 months from current time?
Or is there any official documentation addressing this?