以下是计算总带宽消耗量的代码(值每秒更新和增加)。但是,我希望它计算网络速度,这可以通过每秒从新的减去先前的值来实现。我如何实现这一目标?
public class Browser extends Activity {
private WebView mWebView;
private Handler mHandler = new Handler();
private long mStartRX = 0;
private long mStartTX = 0;
private final Runnable mRunnable = new Runnable() {
public void run() {
calculate();
mHandler.postDelayed(mRunnable, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl("http://www.google.com");
//
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);
}
//
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
// Loading Spinner and status
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.pb3).setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.pb3).setVisibility(View.GONE);
}
};
public void reload(View view) { // Reload Button
mHandler.removeCallbacks(mRunnable);
mHandler.postDelayed(mRunnable, 1000);
TextView RX = (TextView) findViewById(R.id.RX);
TextView TX = (TextView) findViewById(R.id.TX);
TX.setText(TrafficStats.getTotalRxBytes() + "");
RX.setText(TrafficStats.getTotalRxPackets() + "");
android.webkit.CookieManager.getInstance().removeAllCookie();
mWebView.reload();
}
private void calculate() {
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
}
@Override
public void onBackPressed() {
Intent intent = new Intent(this, Advanced.class);
startActivity(intent);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intent, 0);
overridePendingTransition(0, 0); // 0 for no animation
}
}
答案 0 :(得分:0)
使用两个全局变量来存储最后收到的数据。
<div class="tabs-top tabs-background-positive tabs-color-light">
<div class="tabs "style="top:0px;">