Android-将NetworkStatsManager的已接收和已发送字节重置为零

时间:2019-06-23 13:28:44

标签: android networkstatsmanager

是否可以使用NetworkStatsManager为所有应用重置接收和发送的字节?

我正在使用以下代码来获取设备上安装的应用的网络使用情况。

 private void getNetworkStatsBuckets(PackageManager pm) throws PackageManager.NameNotFoundException, RemoteException {
    NetworkStatsManager networkStatsManager = (NetworkStatsManager) getSystemService(Context.NETWORK_STATS_SERVICE);
    TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String subscriberId = manager.getSubscriberId();
    List<PackageInfo> packinfos = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
    for (PackageInfo pInfo : packinfos) {
        String[] premissions = pInfo.requestedPermissions;
        if (premissions != null && premissions.length > 0) {
            for (String premission : premissions) {
                if ("android.permission.INTERNET".equals(premission)) {
                    ApplicationInfo info = pm.getApplicationInfo(pInfo.packageName, 0);
                    int uid = info.uid;
                    NetworkStats networkStats = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI, subscriberId, last_clear_time_minus_4_hours, System.currentTimeMillis(), uid);
                    long rxBytes = 0, txBytes = 0;
                    while (networkStats.hasNextBucket()) {
                        NetworkStats.Bucket bucket = new NetworkStats.Bucket();
                        networkStats.getNextBucket(bucket);
                        rxBytes += bucket.getRxBytes();
                        txBytes += bucket.getTxBytes();
                    }

                   // display the apps according to Received and Transmitted bytes in an ascending order. 
                }
            }
        }
    }
    return;
}

我的问题是我想将所有应用程序的网络使用率重置为零。有什么解决方法吗?

非常感谢您的帮助。 预先感谢。

0 个答案:

没有答案