本文主要是介绍Android下执行linux命令,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近在开发过程中 使用了几个命令来对 手机的文件的权限进行修改;现在记录一下:
用到的方法:
1:判断是否有Root权限;
/**
* 判断当前手机是否有ROOT权限
* @return
*/
public static boolean isRoot(){
boolean bool = false;
try{
if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists())){
bool = false;
} else {
bool = true;
}
Log.e("判断手机是否root:", "bool = " + bool);
} catch (Exception e) {
}
return bool;
}
2:对手机进行重启(建议是6.0系统一下使用)
/**
* LS:手机重启命令
*/
// Save save;
public static void ResetPhone(Context context){
save = new Save(context);
String cmd = "su -c reboot";
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
new AlertDialog.Builder(context).setTitle("Error").setMessage(
e.getMessage()).setPositiveButton("OK", null).show();
}
}
3:手机关机命令
/**
* LS:手机关机
*/
public static void ClosePhone(){
//LS:关机命令
// Log.v(TAG, "system service->reboot");
// SystemProperties.set("ctl.start", "system_reboot");
// System.set("ctl.start", "system_reboot");
// Log.e("---1111","-----11111111111");
String[] commands = new String[] { "su","-c","reboot -p"};
Process process = null;
DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("su");
// Log.e("---22222222","-----22222222222222222222");
dataOutputStream = new DataOutputStream(process.getOutputStream());
int length = commands.length;
for (int i = 0; i < length; i++) {
dataOutputStream.writeBytes(commands[i] + "\n");
}
// Log.e("---3333333333","-----333333333333333333");
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
// Log.e("---4444444444444","-----4444444444444444");
} catch (Exception e) {
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
// Log.e("---5555555","-----55555555555555555");
process.destroy();
} catch (Exception e) {
}
}
}
4:在开发VPN用到的要对安装虚拟网卡的文件权限进行修改;
命令: String[] commands = new String[] { "mount -o remount,rw /system",
"/system/bin/sh", "-c", "chmod -R 777 /etc/ppp/ip-up-vpn",
"mount -o remount,ro /system" };
代码:
/**
* LS:获取虚拟网卡权限
*/
public static void GetVpnPerssion(Context context) {
String[] commands = new String[] { "mount -o remount,rw /system",
"/system/bin/sh", "-c", "chmod -R 777 /etc/ppp/ip-up-vpn",
"mount -o remount,ro /system" };
Process process = null;
DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
int length = commands.length;
for (int i = 0; i < length; i++) {
dataOutputStream.writeBytes(commands[i] + "\n");
}
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
} catch (Exception e) {
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
process.destroy();
} catch (Exception e) {
}
}
}
总结: 对于在代码中执行命令 ;一般要用到这段代码;里面放命令就可以了; 比着直接在Thread中执行:Runtime效果要好很多;并且兼容性更强!
通用代码:
public static void Commands(Context context) {
String[] commands = new String[] { "这个地方填入要执行的命令即可!!!!!!" };
Process process = null;
DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
int length = commands.length;
for (int i = 0; i < length; i++) {
dataOutputStream.writeBytes(commands[i] + "\n");
}
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
} catch (Exception e) {
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
process.destroy();
} catch (Exception e) {
}
}
}
这篇关于Android下执行linux命令的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!