1、實現思路:
一、Android提供有獲取當前總流量的方法
二、上一秒 減去 下一面的流量差即是網速
三、注意計算
2、計算網速的工具類:html
package imcs.cb.com.viewapplication.utils; import android.content.Context; import android.net.TrafficStats; import android.os.Handler; import android.os.Message; import java.util.Timer; import java.util.TimerTask; /** * Created by Ricky on 2016/10/13. */ public class NetWorkSpeedUtils { private Context context; private Handler mHandler; private long lastTotalRxBytes = 0; private long lastTimeStamp = 0; public NetWorkSpeedUtils(Context context, Handler mHandler){ this.context = context; this.mHandler = mHandler; } TimerTask task = new TimerTask() { @Override public void run() { showNetSpeed(); } }; public void startShowNetSpeed(){ lastTotalRxBytes = getTotalRxBytes(); lastTimeStamp = System.currentTimeMillis(); new Timer().schedule(task, 1000, 1000); // 1s後啓動任務,每2s執行一次 } private long getTotalRxBytes() { return TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//轉爲KB } private void showNetSpeed() { long nowTotalRxBytes = getTotalRxBytes(); long nowTimeStamp = System.currentTimeMillis(); long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒轉換 long speed2 = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 % (nowTimeStamp - lastTimeStamp));//毫秒轉換 lastTimeStamp = nowTimeStamp; lastTotalRxBytes = nowTotalRxBytes; Message msg = mHandler.obtainMessage(); msg.what = 100; msg.obj = String.valueOf(speed) + "." + String.valueOf(speed2) + " kb/s"; mHandler.sendMessage(msg);//更新界面 } }
3、使用:java
private Handler mHnadler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 100: tv_speed.setText("當前網速: " + msg.obj.toString()); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_speed = (TextView) findViewById(R.id.tv_speed); new NetWorkSpeedUtils(this,mHnadler).startShowNetSpeed(); }
參考於:https://blog.csdn.net/xueshao110/article/details/82766390android
//-------------------------------------------------------app
private long rxtxTotal =0; private DecimalFormat showFloatFormat =new DecimalFormat("0.00"); public void updateViewData() { long tempSum = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes(); long rxtxLast = tempSum -rxtxTotal; double totalSpeed= rxtxLast *1000 /2000d; rxtxTotal = tempSum; wangsu.setText("當前網速:" + showSpeed(totalSpeed)); //設置顯示當前網速 } private String showSpeed(double speed) { String speedString; if (speed >=1048576d) { speedString =showFloatFormat.format(speed /1048576d) +"MB/s"; }else { speedString =showFloatFormat.format(speed /1024d) +"KB/s"; } return speedString; } 做者:一曲煙雨伊人醉 連接:https://www.jianshu.com/p/377ffb0f24ad 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。
而後開啓計時器 輪詢訪問updateViewData()方法便可 實現ide
參考:https://www.jianshu.com/p/377ffb0f24ad工具
//----------------------------------------------------------------ui
https://www.csdn.net/gather_27/MtTakg1sNzQ3MC1ibG9n.htmlthis
-
package imcs.cb.com.viewapplication.utils;
-
-
import android.content.Context;
-
import android.net.TrafficStats;
-
import android.os.Handler;
-
import android.os.Message;
-
-
import java.util.Timer;
-
import java.util.TimerTask;
-
-
/**
-
* Created by Ricky on 2016/10/13.
-
*/
-
public class NetWorkSpeedUtils {
-
private Context context;
-
private Handler mHandler;
-
-
private long lastTotalRxBytes = 0;
-
private long lastTimeStamp = 0;
-
-
public NetWorkSpeedUtils(Context context, Handler mHandler){
-
this.context = context;
-
this.mHandler = mHandler;
-
}
-
-
TimerTask task = new TimerTask() {
-
@Override
-
public void run() {
-
showNetSpeed();
-
}
-
};
-
-
public void startShowNetSpeed(){
-
lastTotalRxBytes = getTotalRxBytes();
-
lastTimeStamp = System.currentTimeMillis();
-
new Timer().schedule(task, 1000, 1000); // 1s後啓動任務,每2s執行一次
-
-
}
-
-
private long getTotalRxBytes() {
-
return TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/ 1024); //轉爲KB
-
}
-
-
private void showNetSpeed() {
-
long nowTotalRxBytes = getTotalRxBytes();
-
long nowTimeStamp = System.currentTimeMillis();
-
long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp)); //毫秒轉換
-
long speed2 = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 % (nowTimeStamp - lastTimeStamp)); //毫秒轉換
-
-
lastTimeStamp = nowTimeStamp;
-
lastTotalRxBytes = nowTotalRxBytes;
-
-
Message msg = mHandler.obtainMessage();
-
msg.what = 100;
-
msg.obj = String.valueOf(speed) + "." + String.valueOf(speed2) + " kb/s";
-
mHandler.sendMessage(msg); //更新界面
-
}
-
}