Requirement: Test whether the yellow and green lights of the Ethernet port of the Android device are normal
ThreadUtils.runOnSubThread(new Runnable() {
@Override
public void run() {
Log.d("coco","test network 1000m start.");
LinuxCommandRunner.exec("su");
LinuxCommandRunner.exec("ethtool -s eth0 speed 1000 duplex full");
Log.d("coco","test network 1000m end.");
}
});
ThreadUtils is a thread pool tool, you can also use new Thread directly.
LinuxCommandRunner is a tool class for executing Linux instructions.
ThreadUtils.java
package com.xxxxx.xxxxx;
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ThreadUtils {
private static final Handler sHandler = new Handler(Looper.getMainLooper());
private static final Executor sExecutor = Executors.newSingleThreadExecutor();
public static void runOnSubThread(Runnable runnable){
sExecutor.execute(runnable);
}
public static void runOnUIThread(Runnable runnable){
sHandler.post(runnable);
}
public static void shutdown(){
sExecutor.shutdown();
}
}
LinuxCommandRunner.java
package com.rockchip.devicetest.utils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LinuxCommandRunner {
private static final String TAG = LinuxCommandRunner.class.getSimpleName();
public static String runCommand(String[] args) {
//Linux command to be executed
String linuxCommand = "timeout 0.5s top";
try {
//Execute Linux instructions
Process process = Runtime.getRuntime().exec(linuxCommand);
//Read the output of the Linux command
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
//Print execution results
Log.d(TAG,output.toString());
// Check execution results
int exitCode = process.waitFor();
if (exitCode == 0) {
Log.d(TAG,"Linux command executed successfully");
} else {
Log.d(TAG,"Linux command execution failed");
}
return output.toString();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
public static void exec(String command){
try {
Runtime.getRuntime().exec(new String[]{"sh","-c",command});
} catch (IOException e) {
e.printStackTrace();
}
}
}