3

我编写了一个代码来获取网络使用情况,但是 queryDetailsForUid(int networkType, StringsubscriberId,long startTime, long endTime, int uid) 这个方法总是返回旧数据。如何使用NetworkstatsManagerAPI 获取更新的数据?

    try {
            NetworkStats  networkStats = networkStatsManager.queryDetailsForUid(
                            ConnectivityManager.TYPE_WIFI,
                            "",
                    0, System.currentTimeMillis()
                    ,
                    packageUid);

            NetworkStats.Bucket bucket = new NetworkStats.Bucket();
            networkStats.getNextBucket(bucket);
            Log.d("RX  "+packageUid," "+bucket.getRxBytes());
            Log.d("TX  "+packageUid," "+bucket.getTxBytes());

    } catch(Exception e){

    }
4

1 回答 1

3

我使用了相同的方法,它不返回旧数据。您可以使用以下方法测试存储桶的日期和时间:

Log.d(TAG,"StartTime: "+bucket.getStartTimeStamp());
Log.d(TAG,"EndTime: "+bucket.getEndTimeStamp());

检查结束 Timestamp ,你会看到当前的日期和时间。AFAIK,每个桶是 2 小时。

完整代码:

            do {
            NetworkStats.Bucket bucket = new NetworkStats.Bucket();
            networkStatsByApp.getNextBucket(bucket);
            Log.d(TAG, "Bytes Recieved" + bucket.getRxBytes());
            Log.d(TAG, "Bytes Transfered" + bucket.getTxBytes());
            Log.d(TAG, "Bucket UID" + bucket.getUid());
            Log.d(TAG,"StartTime: "+bucket.getStartTimeStamp());
            Log.d(TAG,"EndTime: "+bucket.getEndTimeStamp());
            totalBuckets++;

        }while (networkStatsByApp.hasNextBucket());
于 2016-11-11T19:24:46.357 回答