import android.content.Context;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class LinuxCmdUtil {
private static final String closeEth0="busybox ifconfig eth0 down";
private static final String openEth0="busybox ifconfig eth0 up";
//String val = execRootCmd("cat /sys/class/amhdmitx/amhdmitx0/cec_active");
public static void CloseEth0(){
String val = execRootCmd(closeEth0);
}
public static void openEth0(){
String val = execRootCmd(openEth0);
}
public static void startApp(){
String startAppString="pm start -n [package+MainActivity]";
String val = execRootCmd(startAppString);
}
private static final String getCateWayIp=" cat /proc/net/arp";//获得网关Ip的Linux指令
public static String getCateWayIp(){
String val = LinuxCmdUtil.execRootCmd(getCateWayIp);
return val;
}
/**
*重启
*/
public static void restart(){
LinuxCmdUtil.execRootCmd("reboot");
}
public static void installApK(String installPath,Context mContext){
// 核心是下面几句代码
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setDataAndType(Uri.fromFile(new File(installPath)),
// "application/vnd.android.package-archive");
// mainActivity.startActivity(intent);
StringBuilder installCmdBuilder=new StringBuilder("pm install -r ");
installCmdBuilder.append(installPath);
String val = execRootCmd(installCmdBuilder.toString());
}
public static String execRootCmd(String cmd) {
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
dis = new DataInputStream(p.getInputStream());
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while ((line = dis.readLine()) != null) {
result += line;
}
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
linux命令执行可参考:http://www.jianshu.com/p/603f29a6a513