Android中利用TrafficStats类获取手机接收和发送的数据字节数,然后按秒监听,获取这一秒的数据,减去上一秒的数据,即可以粗略得到当前网速,此程序为监测手机mobile和wifi的网速。
long txB = TrafficStats.getTotalTxBytes(); 为手机当前发送的字节数 long rxB = TrafficStats.getTotalRxBytes(); 为手机当前接收的字节数
网速显示为小数点后两位(四舍五入),用到BigDecimal类实现:
public double formatDouble(Double d) {
BigDecimal b = new BigDecimal(d);
return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //Keep two decimal places
}
当然可以修改为用户可控的小数点有效位,如保留小数点后n为,即可以改为:
public double formatDouble(Double d,int n) {
BigDecimal b = new BigDecimal(d);
return b.setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue(); //Keep n decimal places
}
用TimerTask实现间隔1s获取一次手机接收和发送的数据字节数,用Handler去刷新UI。即如下代码:
class TrafficTimeTask extends TimerTask { @Override public void run() { long txB = TrafficStats.getTotalTxBytes(); long rxB = TrafficStats.getTotalRxBytes(); Bundle bundle = new Bundle(); bundle.putLong(TXB, txB / 1024); //Byte change to KB bundle.putLong(RXB, rxB / 1024); Message message = new Message(); message.what = MSG_TRAFFIC; message.setData(bundle); if (handler != null) { handler.sendMessage(message); } } } class TrafficHandler extends Handler { long lastTxB = 0; //last seconds transferred byte long lastRxB = 0; //last seconds received byte. TrafficHandler() { lastRxB = TrafficStats.getTotalRxBytes() / 1024; //change to KB lastTxB = TrafficStats.getTotalTxBytes() / 1024; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MSG_TRAFFIC: if (msg.getData() != null) { long txb = msg.getData().getLong(TXB); long rxb = msg.getData().getLong(RXB); double speedTx = txb - lastTxB; double speedRx = rxb - lastRxB; String txStr = ""; String rxStr = ""; //DecimalFormat dformat = new DecimalFormat("0.00"); //Keep two decimal places if (speedTx > 1000) { speedTx /= 1024.0; //txStr = dformat.format(speedTx) + "MB/s"; txStr = formatDouble(speedTx) + "MB/s"; } else { //txStr = dformat.format(speedTx) + "KB/s"; txStr = formatDouble(speedTx) + "KB/s"; } if (speedRx > 1000) { speedRx /= 1024.0; //rxStr = dformat.format(speedRx) + "MB/s"; rxStr = formatDouble(speedRx) + "MB/s"; } else { //rxStr = dformat.format(speedRx) + "KB/s"; rxStr = formatDouble(speedRx) + "KB/s"; } String trfStr = "Tx: " + txStr + " /Rx: " + rxStr; Log.d(TAG, trfStr); if (traffic != null) { traffic.setText(trfStr); } lastRxB = rxb; lastTxB = txb; } break; } } }
在Activity中完整代码为:
public class TrafficActivity extends AppCompatActivity { private final String TAG = "traffic"; private static final int MSG_TRAFFIC = 0; private static final String RXB = "rx_bytes"; private static final String TXB = "tx_bytes"; private TextView traffic = null; private Timer trafficTimer = null; private TrafficHandler handler = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_traffic); traffic = (TextView) findViewById(R.id.tv_traffic); handler = new TrafficHandler(); } @Override protected void onResume() { super.onResume(); if (trafficTimer == null) { trafficTimer = new Timer(); trafficTimer.schedule(new TrafficTimeTask(), 1000, 1000); //execute every 1 second. } } @Override protected void onPause() { super.onPause(); if (trafficTimer != null) { trafficTimer.cancel(); trafficTimer = null; } } @Override protected void onDestroy() { super.onDestroy(); } class TrafficTimeTask extends TimerTask { @Override public void run() { long txB = TrafficStats.getTotalTxBytes(); long rxB = TrafficStats.getTotalRxBytes(); Bundle bundle = new Bundle(); bundle.putLong(TXB, txB / 1024); //Byte change to KB bundle.putLong(RXB, rxB / 1024); Message message = new Message(); message.what = MSG_TRAFFIC; message.setData(bundle); if (handler != null) { handler.sendMessage(message); } } } class TrafficHandler extends Handler { long lastTxB = 0; //last seconds transferred byte long lastRxB = 0; //last seconds received byte. TrafficHandler() { lastRxB = TrafficStats.getTotalRxBytes() / 1024; //change to KB lastTxB = TrafficStats.getTotalTxBytes() / 1024; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MSG_TRAFFIC: if (msg.getData() != null) { long txb = msg.getData().getLong(TXB); long rxb = msg.getData().getLong(RXB); double speedTx = txb - lastTxB; double speedRx = rxb - lastRxB; String txStr = ""; String rxStr = ""; //DecimalFormat dformat = new DecimalFormat("0.00"); //Keep two decimal places if (speedTx > 1000) { speedTx /= 1024.0; //txStr = dformat.format(speedTx) + "MB/s"; txStr = formatDouble(speedTx) + "MB/s"; } else { //txStr = dformat.format(speedTx) + "KB/s"; txStr = formatDouble(speedTx) + "KB/s"; } if (speedRx > 1000) { speedRx /= 1024.0; //rxStr = dformat.format(speedRx) + "MB/s"; rxStr = formatDouble(speedRx) + "MB/s"; } else { //rxStr = dformat.format(speedRx) + "KB/s"; rxStr = formatDouble(speedRx) + "KB/s"; } String trfStr = "Tx: " + txStr + " /Rx: " + rxStr; Log.d(TAG, trfStr); if (traffic != null) { traffic.setText(trfStr); } lastRxB = rxb; lastTxB = txb; } break; } } } public double formatDouble(Double d) { BigDecimal b = new BigDecimal(d); return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //Keep two decimal places } }