进到adb中执行 am get-net-speed
$adb shell
$am get-net-speed
diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
index 9f3a7e9..67580b8 100644
--- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
+++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
@@ -77,6 +77,9 @@ import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
+import android.os.Handler;
+import android.os.Message;
+import android.os.Looper;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.ArrayMap;
@@ -103,6 +106,8 @@ import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -148,6 +153,9 @@ final class ActivityManagerShellCommand extends ShellCommand {
private int mTaskId;
private boolean mIsTaskOverlay;
private boolean mIsLockTask;
+ public static final int NET_SPEED_TIMER_DEFAULT = 101010;
+ private static final int ERROR_CODE = -101011010;
+ private int mMsgWhat = ERROR_CODE;
final boolean mDumping;
@@ -288,6 +296,8 @@ final class ActivityManagerShellCommand extends ShellCommand {
return runNoHomeScreen(pw);
case "wait-for-broadcast-idle":
return runWaitForBroadcastIdle(pw);
+ case "get-net-speed":
+ return initNewWork();
default:
return handleDefaultCommands(cmd);
}
@@ -2865,6 +2875,57 @@ final class ActivityManagerShellCommand extends ShellCommand {
return 0;
}
+ Timer timer = new Timer();
+ NetSpeed netSpeed;
+ PrintWriter pw;
+ int i = 0;
+ int times = 60;
+ private Handler mHandler;
+ TimerTask task = new TimerTask() {
+ @Override
+ public void run() {
+ Message obtainMessage = mHandler.obtainMessage();
+
+ if (mMsgWhat != ERROR_CODE) {
+ obtainMessage.what = mMsgWhat;
+ } else {
+ obtainMessage.what = NET_SPEED_TIMER_DEFAULT;
+ }
+ obtainMessage.obj = netSpeed.getNetSpeed();
+ mHandler.sendMessage(obtainMessage);
+
+ if (i++ >= times) {
+ timer.cancel();
+ pw.close();
+ System.out.println("hejiangzhou timer cancel times = " + times);
+ }
+ }
+ };
+
+ private int initNewWork() throws RemoteException {
+ pw = getOutPrintWriter();
+ netSpeed = new NetSpeed();
+
+ times = SystemProperties.getInt("net.times",60);
+
+ Looper.prepare();
+ mHandler = new Handler(){
+ @Override
+ public void handleMessage(Message msg) {
+ super.handleMessage(msg);
+ if (msg.what == NET_SPEED_TIMER_DEFAULT){
+ String speed = (String) msg.obj;
+ //打印你所需要的网速值,单位默认为kb/s
+ pw.println("current netSpeed = " + speed);
+ pw.flush();
+ }
+ }
+ };
+ timer.schedule(task,1000,1000);
+ Looper.loop();
+ return 0;
+ }
+
private Resources getResources(PrintWriter pw) throws RemoteException {
// system resources does not contain all the device configuration, construct it manually.
Configuration config = mInterface.getConfiguration();
diff --git a/services/core/java/com/android/server/am/NetSpeed.java b/services/core/java/com/android/server/am/NetSpeed.java
new file mode 100644
index 0000000..bd8d8c7
--- /dev/null
+++ b/services/core/java/com/android/server/am/NetSpeed.java
@@ -0,0 +1,84 @@
+package com.android.server.am;
+
+import android.net.TrafficStats;
+import android.util.Log;
+
+public class NetSpeed {
+ private static final String TAG = NetSpeed.class.getSimpleName();
+ private long lastTotalRxBytes = 0;
+ private long lastTotalTxBytes = 0;
+ private long lastTotalRxPackets = 0;
+ private long lastTotalTxPackets = 0;
+ private long lastTimeStamp = 0;
+
+ public String getNetSpeed() {
+ StringBuffer stringBuffer = new StringBuffer();
+ long nowTotalRxBytes = (TrafficStats.getTotalRxBytes() / 1024);
+ long nowTimeStamp = System.currentTimeMillis();
+ long rxSpeed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换
+ lastTotalRxBytes = nowTotalRxBytes;
+ Log.d(TAG,"rxSpeed = " + String.valueOf(rxSpeed) + " kb/s");
+
+ long nowTotalTxBytes = (TrafficStats.getTotalTxBytes() / 1024);
+ long txSpeed = ((nowTotalTxBytes - lastTotalTxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));
+ lastTotalTxBytes = nowTotalTxBytes;
+ Log.d(TAG,"txSpeed = " + String.valueOf(txSpeed) + " kb/s");
+
+ long nowTotalRxPackets = TrafficStats.getTotalRxPackets();
+ long rxPacketsSpeed = ((nowTotalRxPackets - lastTotalRxPackets) * 1000 / (nowTimeStamp - lastTimeStamp));
+ lastTotalRxPackets = nowTotalRxPackets;
+ Log.d(TAG,"rxPacketsSpeed = " + String.valueOf(rxPacketsSpeed) + " kb/s");
+
+ long nowTotalTxPackets = TrafficStats.getTotalTxPackets();
+ long txPacketsSpeed = ((nowTotalTxPackets - lastTotalTxPackets) * 1000 / (nowTimeStamp - lastTimeStamp));
+ lastTotalTxPackets = nowTotalTxPackets;
+ Log.d(TAG,"txPacketsSpeed = " + String.valueOf(txPacketsSpeed) + " kb/s");
+
+ lastTimeStamp = nowTimeStamp;
+ stringBuffer.append(quanToban(leftAlign("rxSpeed:" + String.valueOf(rxSpeed) + " kb/s ",20,' ')));
+ stringBuffer.append(quanToban(leftAlign("txSpeed:" + String.valueOf(txSpeed) + " kb/s ",20,' ')));
+ stringBuffer.append(quanToban(leftAlign("rxPacketsSpeed:" + String.valueOf(rxPacketsSpeed) + " packets/s ",30,' ')));
+ stringBuffer.append(quanToban(leftAlign("txPacketsSpeed:" + String.valueOf(txPacketsSpeed) + " packets/s ",20,' ')));
+ Log.d(TAG,"stringBuffer = " + stringBuffer);
+ return String.valueOf(stringBuffer);
+ }
+
+
+ public static String leftAlign(String str, int len,char c) {
+ char array[] = str.toCharArray();
+ for (int i = 0; i < array.length; i++) {//半角转全角
+ if (array[i] == ' ') {
+ array[i] = '\u3000';
+ } else if (array[i] < '\177') {
+ array[i] = (char) (array[i] + 65248);
+ }
+ }
+ int sub = len - str.length();
+ if (sub <= 0) {
+ return new String(array);//大于等于len返回
+ }
+ char[] temp = new char[len];
+ System.arraycopy(array, 0, temp, 0, str.length());
+ for (int j = str.length();j < len; j++) {//左对齐右填充
+ if (c == ' ') {
+ temp[j] = '\u3000';
+ } else if (c < '\177') {
+ temp[j] = (char) (c + 65248);
+ }
+ }
+ return new String(temp);
+ }
+
+
+ public static String quanToban(String str) {
+ char array[] = str.toCharArray();
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == '\u3000') {
+ array[i] = ' ';
+ } else if (array[i] > '\uFF00' && array[i] < '\uFF5F') {
+ array[i] = (char) (array[i] - 65248);
+ }
+ }
+ return new String(array);
+ }
+}