1

我正在尝试比较使用其他手机热点的手机与手机热点之间的数据使用差异。

在打开热点的手机上,我正在使用此代码来计算热点的数据使用情况(结果显示在 TextView (TextView) findViewById(R.id.data_seller) 上)。我将此电话命名为服务器电话:

private void getNetworkStatsServer() {
    NetworkStatsManager networkStatsManager;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        networkStatsManager = getApplicationContext().getSystemService(NetworkStatsManager.class);
        NetworkStats networkStatsWifi = null;
        NetworkStats networkStatsMobile = null;
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            if (networkStatsManager != null) {
                networkStatsWifi = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI,
                        "", 0, calendar.getTimeInMillis(), UID_TETHERING);
                String suscribeId = "";
                TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
                if (tm != null) {
                        suscribeId = tm.getSubscriberId();
                }
                networkStatsMobile = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_MOBILE,
                        suscribeId, 0, calendar.getTimeInMillis(), UID_TETHERING);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        NetworkStats.Bucket bucket;

        if (networkStatsWifi != null) {
            while (networkStatsWifi.hasNextBucket()) {
                bucket = new NetworkStats.Bucket();
                networkStatsWifi.getNextBucket(bucket);
                mStartTXServer += bucket.getTxBytes();
                mStartRXServer += bucket.getRxBytes();
            }
        }

        if (networkStatsMobile != null) {
            while (networkStatsMobile.hasNextBucket()) {
                bucket = new NetworkStats.Bucket();
                networkStatsMobile.getNextBucket(bucket);
                mStartTXServer += bucket.getTxBytes();
                mStartRXServer += bucket.getRxBytes();
            }
        }
    }
    mHandler.postDelayed(mRunnableServer, 1000);
}

mRunnableServer = new Runnable() {
        public void run() {
            long[] res = new long[2];
            NetworkStatsManager networkStatsManager;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                networkStatsManager = getApplicationContext().getSystemService(NetworkStatsManager.class);
                NetworkStats networkStatsWifi = null;
                NetworkStats networkStatsMobile = null;
                try {
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DATE, 1);
                    if (networkStatsManager != null) {
                        networkStatsWifi = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_WIFI,
                                "", 0, calendar.getTimeInMillis(), UID_TETHERING);
                        networkStatsMobile = networkStatsManager.queryDetailsForUid(ConnectivityManager.TYPE_MOBILE,
                                "", 0, calendar.getTimeInMillis(), UID_TETHERING);
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                NetworkStats.Bucket bucket;

                if (networkStatsWifi != null) {
                    while (networkStatsWifi.hasNextBucket()) {
                        bucket = new NetworkStats.Bucket();
                        networkStatsWifi.getNextBucket(bucket);
                        res[0] += bucket.getTxBytes();
                        res[1] += bucket.getRxBytes();
                    }
                }
                if (networkStatsMobile != null) {
                    while (networkStatsMobile.hasNextBucket()) {
                        bucket = new NetworkStats.Bucket();
                        networkStatsMobile.getNextBucket(bucket);
                        res[0] += bucket.getTxBytes();
                        res[1] += bucket.getRxBytes();
                    }
                }
                if (networkStatsMobile != null || networkStatsWifi != null) {
                    res[0] -= mStartTXServer;
                    res[1] -= mStartRXServer;
                }
            } else {
                res[0] = TrafficStats.getUidTxBytes(UID_TETHERING) - mStartTXServer;
                res[1] = TrafficStats.getUidRxBytes(UID_TETHERING) - mStartRXServer;
            }

            System.out.println("Value of Rx: " + res[0]);
            System.out.println("Value of Tx: " + res[1]);

                ((TextView) findViewById(R.id.data_seller)).setText(String.valueOf(((double) (res[0] + res[1])) / 1048576) + "MB");
                mHandler.postDelayed(mRunnableServer, 10000);
        }
    };

关于使用热点连接互联网的手机,我计算了 Wifi 的总数据使用量。我将此手机命名为客户端手机

private void getNetworkStatsClient() {
    mStartTXClient = TrafficStats.getTotalTxBytes();
    mStartRXClient = TrafficStats.getTotalRxBytes();

    mHandler.postDelayed(mRunnableClient, 1000);
}

mRunnableClient = new Runnable() {
        public void run() {
            long[] res = new long[2];
            res[0] = TrafficStats.getTotalTxBytes() - mStartTXClient;
            res[1] = TrafficStats.getTotalRxBytes() - mStartRXClient;

            System.out.println("Value of Rx: " + res[0]);
            System.out.println("Value of Tx: " + res[1]);

            ((TextView) findViewById(R.id.data_buyer)).setText(String.valueOf(((double) (res[0] + res[1])) / 1048576) + "MB");
            mHandler.postDelayed(mRunnableClient, 10000);
        }
    };

我认为两者的结果或多或少是相同的(更准确地说,两个 runnable 中的 res[0]+res[1] 或多或少相等),因为客户端电话正在使用服务器热点电话。但是,结果完全不同(客户端电话的数据使用量是服务器电话的 50 倍)。你知道为什么吗?

4

1 回答 1

1

让我试着改写一下。

设置:您有一个 AP 和一些用户。那是常规的网络共享。

目标:您想要测量数据/带宽使用情况。即,用户吸了多少数据。

实验:您尝试在 AP 端和用户端测量这种使用情况。

观察:您惊讶地发现,您在 AP 端测量的结果与在用户端测量的不同。

潜在的调查策略:

我看到你有 if-else 取决于 SDK 版本。说实验的时候,你的AP永远是同一个设备,而User永远是另外一个设备,那么每一方使用的可能会使用不同的API。

您是否尝试在具有相同 SDK 并因此使用相同 API 的两台设备上运行您的代码?这不是一个答案,但它可能会提供信息。

于 2018-11-19T10:14:50.543 回答