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

发布于 2024-09-14 06:18:22 字数 4516 浏览 7 评论 0原文

我有一个已编译的可执行文件,应该将其自身从 res 文件夹复制到 /data/data/package-name/ 文件夹中,并更改权限,然后执行。每一步都完成直到最后。输出流似乎正在写入等。除了当我去检查文件系统时,什么也没做。我首先尝试使用“sh”,然后使用“su”(我有一个已root的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!),我将永远感激不已。

I have an compiled executable that is supposed to copy itself from the res folder, and into the /data/data/package-name/ folder, and change the permissions, and then execute. Every step completes all the way to the end. The output stream seems to be writing, etc. Except when I go check the file system, nothing has been done. I first tried with 'sh' then with 'su' (I have a rooted Cyanogen rom).

Here is the code:

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";
    }

  }

If anyone could fix this up for me, (and hopefully use sh!) I would be eternally grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜柠檬 2024-09-21 06:18:22

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

Not you should not use shell commands. The SDK does not include any shell commands, and you can not rely on these working consistently across devices. You definitely can't rely on su working across devices. :}

不羁少年 2024-09-21 06:18:22
    executablePath = getIntent().getData().getPath();

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

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

This line is giving a null pointer exception for me. Apparently the URI (java.net.Uri) returned by getIntent().getData is null. I am trying to work around it and see if I can create the file with the rest of your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文