编写执行linux命令的android应用程序

时间:2010-08-12 17:04:34

标签: android shell exec root

我有一个已编译的可执行文件,它应该从res文件夹复制到/ data / data / package-name /文件夹中,然后更改权限,然后执行。每一步都完成到最后。输出流似乎是在写等等。除非我去检查文件系统,否则什么都没做。我首先尝试'sh'然后用'su'(我有一个生根的Cyanogen rom)。

以下是代码:

public class UnexecutableActivity extends Activity {

    String executablePath;
    TextView outputView;
    private UnexecutableTask mUnexecutableTask;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        executablePath = getIntent().getData().getPath();
        System.out.println(executablePath);
        setContentView(R.layout.main);
        outputView = (TextView)findViewById(R.id.outputView);
        try {
            Process process = Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Runnable runexecutable = new Runnable(){

            @Override
            public void run() {
                mUnexecutableTask = new UnexecutableTask();
                mUnexecutableTask.execute("");
            }

        };

        runOnUiThread(runexecutable);

    }


    private class UnexecutableTask extends AsyncTask<String, String, String> {



        public UnexecutableTask() {
            super();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            outputView.setText(outputView.getText() + "\n" + executablePath + "converted to ");
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            outputView.setText("About to un-executable " + executablePath + " ...");
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            outputView.setText(outputView.getText() + "\n" + values[0]);
        }

        @Override
        protected String doInBackground(String... params) {
        String bashEscapedPath = executablePath.replace(" ", "\\ ");
        try{
            String[] commands;
            publishProgress("Loading unexecutable...");
            InputStream unexecutableInputStream = getAssets().open("unexecutable");
            FileOutputStream fos = new FileOutputStream(getDir("", MODE_WORLD_WRITEABLE) + "/unexecutable");
             byte[] tmp = new byte[2048];
                int l;
                while ((l = unexecutableInputStream.read(tmp)) != -1) {
                    fos.write(tmp, 0, l);
                }
                fos.flush();
                fos.close();
                unexecutableInputStream.close();

            publishProgress("Changing file permissions...");
            commands = new String[] {"/system/bin/chmod","744", getDir("", MODE_WORLD_WRITEABLE) + "/unexecutable"};
            Process process = Runtime.getRuntime().exec("su");
            StringBuffer res = new StringBuffer();
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            DataInputStream osRes = new DataInputStream(new
                    BufferedInputStream(process.getInputStream()));
            for (String single : commands) {
               os.writeBytes(single + "\n");
               os.flush();
               //publishProgress(String.valueOf(osRes.readByte()));
            }
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();


            publishProgress("Performing un-executable...");
            commands = new String[] {"/data/data/" + getPackageName() + "/unexecutable", bashEscapedPath};
            process = Runtime.getRuntime().exec("su");
            res = new StringBuffer();
            os = new DataOutputStream(process.getOutputStream());
            osRes = new DataInputStream(process.getInputStream());
            for (String single : commands) {
               os.writeBytes(single + "\n");
               os.flush();
            }
            os.writeBytes("exit\n");
            os.flush();
            publishProgress("Finishing...");
            process.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "Success";
    }

  }

如果有人能为我解决这个问题,(并希望用sh!)我会永远感激。

2 个答案:

答案 0 :(得分:1)

不是你不应该使用shell命令。 SDK不包含任何shell命令,您不能依赖这些命令在各种设备上一致地工作。你绝对不能依赖su跨设备工作。 :}

答案 1 :(得分:0)

    executablePath = getIntent().getData().getPath();

这一行给我一个空指针异常。显然,getIntent()返回的URI(java.net.Uri).getData为null。我正在尝试解决它,看看我是否可以使用其余代码创建文件。