代码之家  ›  专栏  ›  技术社区  ›  Bazouk55555

Android:热点手机和使用热点手机的数据使用差异

  •  1
  • Bazouk55555  · 技术社区  · 6 年前

    我试图比较使用另一个手机热点的手机和手机热点的数据使用差异。

    在打开热点的手机上,我用这段代码计算热点的数据使用量(结果显示在TextView(TextView)findViewById(R。卖方id.data)). 我把这个电话命名为服务器电话:

    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倍)。

    1 回复  |  直到 6 年前
        1
  •  1
  •   hartmut    6 年前

    让我试着重新措辞。

    设置: 你有一个AP和一些用户。这是常规系绳。

    目标: 您需要测量数据/带宽使用情况。一、 例如,用户吸了多少数据。

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

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

    潜在调查策略:

    我看你有,如果还有其他取决于SDK版本。假设在实验时,你的AP总是同一个设备,而用户总是另一个设备,那么每一方使用的API可能不同。

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

    推荐文章