【问题标题】:Cannot run the shell command without rooting android没有root android无法运行shell命令
【发布时间】:2014-03-11 08:11:38
【问题描述】:

在将此代码作为 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)

【问题讨论】:

    标签: android shell ioexception broken-pipe


    【解决方案1】:

    您尝试获取管理员。

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

    非Rooting设备被阻止。
    此政策适用于 android。

    【讨论】:

    • 如果已经提到你提到的特定行,你能否像在我的代码中一样指定其背后的原因。请参考返回功能。
    • @Shubhankar 我不明白你的意思。
    • 看看我的代码。在后面的函数中,我已经指定了你提到的那一行。
    • 阅读此链接第 171 行。[link]gitorious.org/ginger/libcore/source/…
    • 这个错误是你或你的应用没有root权限。并阅读此页面。 [链接]stackoverflow.com/questions/17615412/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2016-05-28
    • 2014-01-18
    相关资源
    最近更新 更多