public class NetSpeed { private static final String TAG = NetSpeed.class.getSimpleName(); private long lastTotalRxBytes = 0; private long lastTimeStamp = 0; public String getNetSpeed(int uid) { long nowTotalRxBytes = getTotalRxBytes(uid); long nowTimeStamp = System.currentTimeMillis(); long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒轉換 lastTimeStamp = nowTimeStamp; lastTotalRxBytes = nowTotalRxBytes; return String.valueOf(speed) + " kb/s"; } //getApplicationInfo().uid public long getTotalRxBytes(int uid) { return TrafficStats.getUidRxBytes(uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//轉爲KB }
public class NetSpeedTimer { private long defaultDelay = 1000; private long defaultPeriod = 1000; private static final int ERROR_CODE = -101011010; private int mMsgWhat = ERROR_CODE; private NetSpeed mNetSpeed; private Handler mHandler; private Context mContext; private SpeedTimerTask mSpeedTimerTask; public static final int NET_SPEED_TIMER_DEFAULT = 101010; public NetSpeedTimer(Context context, NetSpeed netSpeed, Handler handler) { this.mContext = context; this.mNetSpeed = netSpeed; this.mHandler = handler; } public NetSpeedTimer setDelayTime(long delay) { this.defaultDelay = delay; return this; } public NetSpeedTimer setPeriodTime(long period) { this.defaultPeriod = period; return this; } public NetSpeedTimer setHanderWhat(int what) { this.mMsgWhat = what; return this; } /** * 開啓獲取網速定時器 */ public void startSpeedTimer() { Timer timer = new Timer(); mSpeedTimerTask = new SpeedTimerTask(mContext, mNetSpeed, mHandler, mMsgWhat); timer.schedule(mSpeedTimerTask, defaultDelay, defaultPeriod); } /** * 關閉定時器 */ public void stopSpeedTimer() { if (null != mSpeedTimerTask) { mSpeedTimerTask.cancel(); } } /** * @author * 靜態內部類 */ private static class SpeedTimerTask extends TimerTask { private int mMsgWhat; private NetSpeed mNetSpeed; private Handler mHandler; private Context mContext; public SpeedTimerTask(Context context, NetSpeed netSpeed, Handler handler, int what) { this.mContext = context; this.mHandler = handler; this.mNetSpeed = netSpeed; this.mMsgWhat = what; } @Override public void run() { // TODO Auto-generated method stub if (null != mNetSpeed && null != mHandler) { Message obtainMessage = mHandler.obtainMessage(); if (mMsgWhat != ERROR_CODE) { obtainMessage.what = mMsgWhat; } else { obtainMessage.what = NET_SPEED_TIMER_DEFAULT; } obtainMessage.obj = mNetSpeed.getNetSpeed(mContext .getApplicationInfo().uid); mHandler.sendMessage(obtainMessage); } } } }
調用:java
private void initNewWork() { //建立NetSpeedTimer實例 mNetSpeedTimer = new NetSpeedTimer(mContext, new NetSpeed(), mHandler).setDelayTime(1000).setPeriodTime(2000); //在想要開始執行的地方調用該段代碼 mNetSpeedTimer.startSpeedTimer(); } private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == NetSpeedTimer.NET_SPEED_TIMER_DEFAULT){ String speed = (String) msg.obj; //打印你所須要的網速值,單位默認爲kb/s Log.i(TAG, "current net speed = " + speed); } } }; @Override protected void onDestroy() { // TODO Auto-generated method stub if(null != mNetSpeedTimer){ mNetSpeedTimer.stopSpeedTimer(); } super.onDestroy(); }