如何在APK程序里执行linux命令1
发布网友
发布时间:2023-10-14 11:05
我来回答
共1个回答
热心网友
时间:2024-12-03 04:37
Android的底层是Linux内核,因此在shell环境下可以运行Linux命令,尤其是经过root处理的android系统,基本上可以通过调用Linux命令完全控制手机,下面的RootCmd.java代码可以实现运行Linux外部命令。
package my.android.code;
import android.os.Environment;
import dalvik.annotation.Signature;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
public final class RootCmd
{
//执行linux命令并且输出结果
protected static Vector execRootCmd(String paramString)
{
Vector localVector = new Vector();
try
{
Process localProcess = Runtime.getRuntime().exec("su ");//经过Root处理的android系统即有su命令
OutputStream localOutputStream = localProcess.getOutputStream();
DataOutputStream localDataOutputStream = new DataOutputStream(localOutputStream);
InputStream localInputStream = localProcess.getInputStream();
DataInputStream localDataInputStream = new DataInputStream(localInputStream);
String str1 = String.valueOf(paramString);
String str2 = str1 + "\n";
localDataOutputStream.writeBytes(str2);
localDataOutputStream.flush();
String str3 = localDataInputStream.readLine();
localVector.add(str3);
localDataOutputStream.writeBytes("exit\n");
localDataOutputStream.flush();
localProcess.waitFor();
return localVector;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
//执行linux命令但不关注结果输出
protected static int execRootCmdSilent(String paramString)
{
try
{
Process localProcess = Runtime.getRuntime().exec("su");
Object localObject = localProcess.getOutputStream();
DataOutputStream localDataOutputStream = new DataOutputStream((OutputStream)localObject);
String str = String.valueOf(paramString);
localObject = str + "\n";
localDataOutputStream.writeBytes((String)localObject);
localDataOutputStream.flush();
localDataOutputStream.writeBytes("exit\n");
localDataOutputStream.flush();
localProcess.waitFor();
localObject = localProcess.exitValue();
return localObject;
}
catch (Exception localException)
{
localException.printStackTrace();
}
}
//判断机器Android是否已经root,即是否获取root权限
protected static boolean haveRoot()
{
int i = execRootCmdSilent("echo test"); //通过执行测试命令来检测
if (i != -1) return true;
retrun false;
}
}