我正在尝试使用NetworkStatsManager类来检索我的应用程序的数据使用情况。我想每隔一个小时(一个小时的间隔)进行一次跟踪,并且在我阅读queryDetails时,我无法选择它,因为它只能使用2-3个小时。
当我尝试使用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;
}
使用两个不同的startTime调用getNetworkStats:
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)
返回相同的数据。 为什么?