阅读命令输出java.lang.StringIndexOutOfBoundsException时Android App崩溃

时间:2017-03-04 21:18:49

标签: java android

我想通过我的Android应用程序运行root命令,该应用程序包含后台服务,使用下面的函数:

    public static String runAsRoot(String cmd) {
        String output = "";
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
            DataOutputStream stdin = new DataOutputStream(p.getOutputStream());
            stdin.writeBytes(cmd + "\nexit\n");
            InputStream stdout = p.getInputStream();
            byte[] buffer = new byte[128];
            int read;
            int loopCount = 0;

//read method will wait forever if there is nothing in the stream
//so we need to read it in another way than while((read=stdout.read(buffer))>0)
            while (true) {
                try {
                    loopCount++;
                    read = stdout.read(buffer);
                    output += new String(buffer, 0, read);
                    if (read < 128 || loopCount > 5) {
                        //we have read everything
                        break;
                    }
                }catch (Exception e){

                }
            }

            //Close streams
            stdin.close();
            stdout.close();
            p.destroy();

            Log.e("Command output:", output);
        } catch (Exception e) {
            e.printStackTrace();
            output = "exceptionFound";
        }

        return output;
    }

我通过创建线程多次调用此函数:

    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            try {

//                                if (notOnTop>10){
//                                    runAsRoot("reboot now");
//                                }

                                int isRunning = controlTopApp();
                                if (isRunning == 0) {
                                    Log.d("Control_OnTop", "Attendance notOnTop,bringToFront");
                                    runAsRoot("am start -n berk.can.myapp.anotherapp/.MainActivity");
                                } else if (isRunning == 1) {
                                    Log.d("Control_OnTop", "Attendance");
                                }

                            } catch (Exception e) {
                                Log.e("Runnable App E.", Log.getStackTraceString(e));
                            }

                        }
                    }).start();

我想得到root命令的输出,所以我给read一个指定的值(这里是128)。但过了一段时间它就会出现这个错误并且我的服务被杀了:

02-28 16:34:41.751 22377-22759/? W/System.err: java.lang.StringIndexOutOfBoundsException: length=128; regionStart=0; regionLength=-1
02-28 16:34:41.760 22377-22759/? W/System.err:     at java.lang.String.failedBoundsCheck(String.java:508)
02-28 16:34:41.760 22377-22759/? W/System.err:     at java.lang.String.<init>(String.java:225)
02-28 16:34:41.760 22377-22759/? W/System.err:     at java.lang.String.<init>(String.java:149)
02-28 16:34:41.760 22377-22759/? W/System.err:     at berk.can.myapp.controllerservice.MyService.runAsRoot(MyService.java:271)
02-28 16:34:41.760 22377-22759/? W/System.err:     at berk.can.myapp.controllerservice.MyService.controlTopApp(MyService.java:176)
02-28 16:34:41.760 22377-22759/? W/System.err:     at berk.can.myapp.controllerservice.MyService.access$000(MyService.java:27)
02-28 16:34:41.760 22377-22759/? W/System.err:     at berk.can.myapp.controllerservice.MyService$TimeTask$1.run(MyService.java:103)
02-28 16:34:41.760 22377-22759/? W/System.err:     at java.lang.Thread.run(Thread.java:818)

是否可以因为同时运行多个线程以及为什么我无法通过try-catch处理Exception并继续运行我的服务?

1 个答案:

答案 0 :(得分:0)

当您到达文件末尾时EOF InputStream.read(...)将返回-1,您必须在构建字符串之前检查,output += new String(buffer, 0, read);

做这样的事情:

while (true) {
    try {
        loopCount++;
        read = stdout.read(buffer);
        if(read == -1){
            break;
        }
        output += new String(buffer, 0, read);
        if (read < 128 || loopCount > 5) {
            //we have read everything
            break;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

应该防止例外。