我正在使用 NetworkStatsManager 类来检索应用程序的数据使用情况。
如果我提供 01.01.2018 作为开始时间,则表明我的手机已发出 5.4 GB。
如果我提供 01.01.2017,它仍然显示 5,4 GB。
这让我假设开始时间存在某种限制。不幸的是,该文档没有提及有关此的任何内容。
那么,我们能回到多少时间呢?
代码:
这是使用 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;
}
正如您在上面看到的,我为该方法提供了一个日历对象。
这就是我获取本年度日历的方式:
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;
}
这将返回 Mon Jan 01 00:00:00 GMT+01:00 2018。
如果我现在将其设置为 2017,则结果与 2018 相同。
难道我做错了什么?
编辑:
我刚刚做了一些测试,限制似乎是3个月。
任何大于 3 个月的开始时间都会产生相同的值。
任何人都可以确认从当前时间开始使其工作超过 3 个月吗?
或者是否有任何官方文档解决这个问题?