3

有没有办法使用 重置所有应用程序的接收和传输字节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;
}

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

任何帮助深表感谢。提前致谢。

4

0 回答 0