从TrafficStats Android计算网络速度

时间:2015-02-15 16:52:44

标签: android

我可以通过TrafficStats在textviews中显示带宽使用率,但我希望每秒显示网络速度。我怎么做到这一点?下面是我的代码

public class MainActivity extends Activity {

    private Handler mHandler = new Handler();
    private long mStartRX = 0;
    private long mStartTX = 0;
    private final Runnable mRunnable = new Runnable() {
        public void run() {

            TextView RX = (TextView) findViewById(R.id.RX);
            TextView TX = (TextView) findViewById(R.id.TX);
            // long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
            // RX.setText(Long.toString(rxBytes));
            long rxBytes = TrafficStats.getTotalRxBytes() - mStartRX;
            RX.setText(Long.toString(rxBytes) + " bytes");
            if (rxBytes >= 1024) {
                // KB or more
                long rxKb = rxBytes / 1024;
                RX.setText(Long.toString(rxKb) + " KBs");
                if (rxKb >= 1024) {
                    // MB or more
                    long rxMB = rxKb / 1024;
                    RX.setText(Long.toString(rxMB) + " MBs");
                    if (rxMB >= 1024) {
                        // GB or more
                        long rxGB = rxMB / 1024;
                        RX.setText(Long.toString(rxGB));
                    }// rxMB>1024
                }// rxKb > 1024
            }// rxBytes>=1024

            // long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
            // TX.setText(Long.toString(txBytes));
            long txBytes = TrafficStats.getTotalTxBytes() - mStartTX;
            TX.setText(Long.toString(txBytes) + " bytes");
            if (txBytes >= 1024) {
                // KB or more
                long txKb = txBytes / 1024;
                TX.setText(Long.toString(txKb) + " KBs");
                if (txKb >= 1024) {
                    // MB or more
                    long txMB = txKb / 1024;
                    TX.setText(Long.toString(txMB) + " MBs");
                    if (txMB >= 1024) {
                        // GB or more
                        long txGB = txMB / 1024;
                        TX.setText(Long.toString(txGB));
                    }// rxMB>1024
                }// rxKb > 1024
            }// rxBytes>=1024

            mHandler.postDelayed(mRunnable, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mStartRX = TrafficStats.getTotalRxBytes();
        mStartTX = TrafficStats.getTotalTxBytes();

        if (mStartRX == TrafficStats.UNSUPPORTED
                || mStartTX == TrafficStats.UNSUPPORTED) {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Uh Oh!");
            alert.setMessage("Your device does not support traffic stat monitoring.");
            alert.show();

        } else {
            mHandler.postDelayed(mRunnable, 1000);

        }
    }
}

0 个答案:

没有答案