packagemy.android.codeimportandroid.os.Environmentimportdalvik.annotation.Signatureimportjava.io.BufferedReaderimportjava.io.DataInputStreamimportjava.io.DataOutputStreamimportjava.io.Fileimportjava.io.FileReaderimportjava.io.InputStreamimportjava.io.OutputStreamimportjava.util.VectorpublicfinalclassRootCmd{//执行linux命令并且输出结果protectedstaticVectorexecRootCmd(StringparamString){VectorlocalVector=newVector();
try{ProcesslocalProcess=Runtime.getRuntime()。
exec(“su”);
//经过Root处理的链侍android系统即有su命令OutputStreamlocalOutputStream=localProcess.getOutputStream();
DataOutputStreamlocalDataOutputStream=newDataOutputStream(localOutputStream);
InputStreamlocalInputStream=localProcess.getInputStream();
DataInputStreamlocalDataInputStream=newDataInputStream(localInputStream);
Stringstr1=String.valueOf(paramString);
Stringstr2=str1+“\n”;localDataOutputStream.writeBytes(str2);
localDataOutputStream.flush();
Stringstr3=localDataInputStream.readLine();
localVector.add(str3);
localDataOutputStream.writeBytes(“exit\n”);
localDataOutputStream.flush();
localProcess.waitFor();
returnlocalVector}catch(ExceptionlocalException){localException.printStackTrace();
}}//执行linux命令但不关注结果输出protectedstaticintexecRootCmdSilent(StringparamString){try{ProcesslocalProcess=Runtime.getRuntime()。
exec(“su”);
ObjectlocalObject=localProcess.getOutputStream();
DataOutputStreamlocalDataOutputStream=newDataOutputStream((OutputStream)localObject);
Stringstr=String.valueOf(paramString);
localObject=str+“\n”;localDataOutputStream.writeBytes((String)localObject);
localDataOutputStream.flush();
localDataOutputStream.writeBytes(“exit\n”);
localDataOutputStream.flush();
localProcess.waitFor();
localObject=localProcess.exitValue();
returnlocalObject}catch(ExceptionlocalException){localException.printStackTrace();
}}//判断机器Android是否已经root,即是否获取root权限protectedstaticbooleanhaveRoot(){inti=execRootCmdSilent(“echotest”);
//通过执行测试命令来检测if(i!=-1)returntrueretrunfalse}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)