开发者

RE :How can we execute a shell script file from my Android Application

I am trying to write an android application that runs a shell commands, or a shell script if that is preferable, and displays the output... can anyone give me in the right direction?

My code is as follows:

void execCommandLine()
    {
        Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        OutputStreamWriter osw = null;

        try
        {

            String[] str={"/system/bin/sh","/data/shTest.sh"};              
                 System.out.println("EXEC STRING");
                 proc = runtime.exec(str);
               osw = new OutputStreamWriter(proc.getOutputStream());
            //osw.write(command);
            osw.flush();
            osw.close();
        }
        catch (IOException ex)
    开发者_开发技巧    {
              Log.e("erre","ioexception");          
            //Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command);
            return;
        }
        finally
        {
            if (osw != null)
            {
                try
                {
                    osw.close();
                }
                catch (IOException e){}
            }
        }

        try 
        {
            proc.waitFor();
        }
        catch (InterruptedException e){}

        if (proc.exitValue() != 0)
        {
            Log.e("erre","interruotexception");         
            //Log.e("execCommandLine()", "Command returned error: " + command + "\n  Exit code: " + proc.exitValue());
        }
    }
     // **************************************

Code is running successfully but I am not getting any output in adb shell logcat would anyone tell me if this script is executed successfully how to get this output in Adb shell.


Have you looked into GScript. It is quite flexible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜