问题描述
我有我正在上一个Android应用程序需要根。它使用根访问一些内部记忆的东西,然后我称之为 LS
上的目录。然后,我从它是否有数据,但如果该目录是空的,应用程序只是挂起伟大的工程读取。有没有解决这个好办法?
这里的code
最终运行运行时间=调用Runtime.getRuntime();
尝试{
//执行su到获得root权限
进程p =的Runtime.exec(苏);
DataOutputStream类输出=新的DataOutputStream类(p.getOutputStream());
output.writeBytes(LS+目录路径+\\ n);
output.flush();
InputStream的标准输出= p.getInputStream();
字节[]缓冲区=新的字节[1024];
INT读;
串出=新的String();
而(真){
//如果LS没有发现文件就挂在这条线
读= stdout.read(缓冲);
OUT + =新的String(缓冲,0,读);
如果(读< 1024){
打破;
}
}
}
对我来说,以下工作:
看到我的<一个href=\"https://bitbucket.org/jholetzeck/ministatus-android/src/0d594b5dbe0f9c838c4a61cb0e339f1ae11cb883/src/de/holetzeck/util/ProcessHelper.java?at=default\"相对=nofollow>过程帮手再例如:
对&LT;整数,字符串&GT; RET = ProcessHelper.runCmd(真实的,苏,-c,ls -l命令);
I have an Android application that I'm working on that requires root. It uses root to access some of the internal memory stuff, and then I call ls
on a directory. I then read from it which works great if there is data, but if the directory is empty, the application just hangs. Is there a good way around this?
Here's the code
final Runtime runtime = Runtime.getRuntime();
try {
// Perform su to get root privileges
Process p = runtime.exec("su");
DataOutputStream output = new DataOutputStream(p.getOutputStream());
output.writeBytes("ls " + directoryPath + "\n");
output.flush();
InputStream stdout = p.getInputStream();
byte[] buffer = new byte[1024];
int read;
String out = new String();
while(true){
//If ls found no files it hangs on this line
read = stdout.read(buffer);
out += new String(buffer, 0, read);
if(read < 1024){
break;
}
}
}
The following works for me:see my process helper and then for example
Pair<Integer, String> ret = ProcessHelper.runCmd(true, "su", "-c", "ls -l");
这篇关于呼唤“LS”在Android上使用根返回任何内容,应用程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!