我正在尝试执行以下方法:
public voID runAsRoot(String[] cmds) throws Exception { Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getoutputStream()); inputStream is = p.getinputStream(); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd+"\n"); int readed = 0; byte[] buff = new byte[4096]; // if cmd requires an output // due to the blocking behavIoUr of read(...) boolean cmdRequiresAnOutput = true; if (cmdRequiresAnOutput) { while( is.available() <= 0) { try { Thread.sleep(200); } catch(Exception ex) {} } while( is.available() > 0) { readed = is.read(buff); if ( readed <= 0 ) break; String seg = new String(buff,0,readed); Log.i("#>", seg); } } } os.writeBytes("exit\n"); os.flush(); }
我使用以下输入调用了此方法:
String[] cmds = {"/system/bin/sendevent /dev/input/event0 1 107 0 \n", "sleep 1", "/system/bin/sendevent /dev/input/event0 1 107 1 \n"}; try { runAsRoot(cmds); } catch (Exception e) { // Todo auto-generated catch block e.printstacktrace(); }
但是在logcat中,我收到以下错误:
07-06 15:19:27.007: E/su(6547): sudb - opening database07-06 15:19:27.007: E/(6547): Couldn't open database: unable to open database file07-06 15:19:27.017: E/su(6547): sudb - Could not open database, prompt user07-06 15:19:47.082: E/su(6547): select Failed with 2: No such file or directory07-06 15:19:47.082: W/su(6547): request rejected (10060->0 /system/bin/sh)
知道是什么问题吗?
解决方法:
su二进制文件似乎有问题,而不是您的应用程序有问题.检查是否可以从“ adb shell”成功运行有根的shell.
如果’adb shell’从一开始就为您提供了一个有根目录的shell,请运行’su 1000’以失去根目录特权,然后运行’su’尝试再次进入有根目录的shell.如果失败,则su无法正常工作.
哦,还有一个相关说明:请确保在另一个线程中运行su,可能是通过Handler或AsyncTask进行的,这样它就不会阻塞您的UI线程.
总结以上是内存溢出为你收集整理的从android活动执行su命令全部内容,希望文章能够帮你解决从android活动执行su命令所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)