Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的Runtime.exec()挂起_Android_Runtime_Exec_Android 2.2 Froyo - Fatal编程技术网

Android中的Runtime.exec()挂起

Android中的Runtime.exec()挂起,android,runtime,exec,android-2.2-froyo,Android,Runtime,Exec,Android 2.2 Froyo,当我尝试以这种方式执行外部脚本时: try { process = Runtime.getRuntime().exec( new String[] { "/system/bin/sh", "./myscript.sh" }, null, "/data/mydir", ); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } catch (SecurityExc

当我尝试以这种方式执行外部脚本时:

try {
    process = Runtime.getRuntime().exec(
        new String[] { "/system/bin/sh", "./myscript.sh" },
        null,
        "/data/mydir",
    );
} catch (IOException e) {
    Log.e(TAG, e.getMessage(), e);
} catch (SecurityException e) {
    Log.e(TAG, e.getMessage(), e);
}
有时脚本会被执行,但大多数情况下,我的应用程序会挂起几秒钟,直到Android说我的应用程序没有响应,需要终止它

我的问题是,可能会发生什么。脚本有时正在运行,没有抛出异常,只是挂起。我不知道发生了什么事。我用的是Froyo(我想是2.2.1)


谢谢

根据文档,您应该阅读流程中的错误和输出流

我想下面这样的事情会解决你的问题

class Reader extends Thread
{
    InputStream is;

    Reader(InputStream is){
        this.is = is;
    }

    public void run()
    {
        try
        {
            InputStreamReader inStreamReader = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(inStreamReader);
            String line=null;
            while ( (line = br.readLine()) != null){
                // log here   
            }
        } catch (IOException ex){
            ex.printStackTrace();  
        }
    }
}
像这样在代码中使用上面的类

try {
    process = Runtime.getRuntime().exec(
        new String[] { "/system/bin/sh", "./myscript.sh" },
        null,
        "/data/mydir",
    );
    Reader err = new Reader(process.getErrorStream());
    Reader output = new Reader(process.getInputStream());

    err.start();
    outout.start();

} catch (IOException e) {
    Log.e(TAG, e.getMessage(), e);
} catch (SecurityException e) {
    Log.e(TAG, e.getMessage(), e);
} finally {
    process.destroy();
}

嗯,我想这和安卓线程有关。你试过在a线程中这样做吗?你这样认为吗?也许,发生了某种僵局,不知道。你说得对,我会给它自己的线索。:-)查看流程类doc developer.android.com/reference/java/lang/process.html。概述似乎很有用,并解释了为什么它可能会阻塞。