我正在尝试比较使用另一部手机的热点和该手机的热点之间的数据使用差异。
在打开热点的手机上,我使用以下代码来计算热点的数据使用率(结果显示在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);
}
};对于使用热点上网的手机,我计算了无线网络的总数据使用量,我将这部手机命名为客户端手机
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+res1应该或多或少相同),因为客户端电话使用的是服务器热点电话。但是,结果完全不同(客户端电话的数据使用量是服务器电话的50倍)。你知道为什么吗?
发布于 2018-11-19 18:14:51
让我试着换个说法。
设置:您有一个AP和一些用户。这是常规的系留。
目标:您想要测量数据/带宽使用情况。也就是说,用户吸收了多少数据。
实验:您尝试在AP端和用户端测量这种使用情况。
观察:你会惊讶地发现,你在AP端测量的东西与你在用户端测量的东西是不一样的。
潜在调查策略:
我看到你有和if-else,这取决于SDK版本。假设在实验时,您的AP总是相同的设备,而用户总是另一个设备,那么每一方使用的API都可能不同。
您是否尝试过在具有相同SDK的两个设备上运行您的代码,因此将使用相同的API?这不是一个答案,但它可能是信息性的。
https://stackoverflow.com/questions/53352247
复制相似问题