没有root权限,无法运行shell命令

时间:2014-03-11 08:11:38

标签: android shell ioexception broken-pipe

在将此代码作为IOException运行时,它给了我一个异常。 每当执行第一个字符串值“sysrw”时,它就会显示错误。 我试图在没有root电话的情况下在android中运行shell命令。 建议修复。感谢。

 public class MainActivity extends Activity {
        Button b1;
        TextView tv1;
        String[] commands = {"sysrw", "cat /mnt/sdcard/abc.txt", "sysro"};
        int time= 2000;
        int i=0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv1 = (TextView) findViewById(R.id.textView1);
            b1= (Button) findViewById(R.id.button1);
            b1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    i++;
                    try {
                        back(commands);
                    } catch (Exception e) {
                        Toast.makeText(getBaseContext(), "Catch block "+i, Toast.LENGTH_LONG).show();
                    }
                    Toast.makeText(MainActivity.this, "Cheers", time).show();
                }
            });
        }
        public void back(String[] cmds) throws Exception {

            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());            
            for (String tmpCmd : cmds) {
                    os.writeBytes(tmpCmd+"\n");
                    tv1.setText("\n"+tmpCmd);
            }           
            os.writeBytes("exit\n");  
            os.flush();
        }

堆栈跟踪如下所述: 堆栈跟踪:

03-09 18:41:10.586: W/System.err(754): java.io.IOException: Broken pipe
03-09 18:41:10.595: W/System.err(754):  at org.apache.harmony.luni.platform.OSFileSystem.write(Native Method)
03-09 18:41:10.595: W/System.err(754):  at dalvik.system.BlockGuard$WrappedFileSystem.write(BlockGuard.java:171)
03-09 18:41:10.605: W/System.err(754):  at java.io.FileOutputStream.write(FileOutputStream.java:300)
03-09 18:41:10.605: W/System.err(754):  at java.io.FileOutputStream.write(FileOutputStream.java:256)
03-09 18:41:10.605: W/System.err(754):  at java.io.DataOutputStream.writeBytes(DataOutputStream.java:167)
03-09 18:41:10.605: W/System.err(754):  at com.example.abc.MainActivity.back(MainActivity.java:46)
03-09 18:41:10.605: W/System.err(754):  at com.example.abc.MainActivity$1.onClick(MainActivity.java:31)
03-09 18:41:10.605: W/System.err(754):  at android.view.View.performClick(View.java:2485)
03-09 18:41:10.615: W/System.err(754):  at android.view.View$PerformClick.run(View.java:9080)
03-09 18:41:10.615: W/System.err(754):  at android.os.Handler.handleCallback(Handler.java:587)
03-09 18:41:10.615: W/System.err(754):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-09 18:41:10.625: W/System.err(754):  at android.os.Looper.loop(Looper.java:123)
03-09 18:41:10.625: W/System.err(754):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-09 18:41:10.625: W/System.err(754):  at java.lang.reflect.Method.invokeNative(Native Method)
03-09 18:41:10.625: W/System.err(754):  at java.lang.reflect.Method.invoke(Method.java:507)
03-09 18:41:10.625: W/System.err(754):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-09 18:41:10.625: W/System.err(754):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-09 18:41:10.625: W/System.err(754):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

你试试得到管理员。

Process p = Runtime.getRuntime().exec("su");

NonRooting设备被阻止 这个安全策略的策略。