【问题标题】:Writing android app that executes linux commands编写执行 linux 命令的 android 应用程序
【发布时间】:2011-03-29 01:32:31
【问题描述】:

我有一个编译后的可执行文件,它应该从 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!)我将永远感激不尽。

【问题讨论】:

  • 我对 pastebin 不太熟悉 - 你能把它做个链接吗?
  • 为什么不把代码放在这里? SO支持语法高亮等。
  • 您正在运行什么版本的 CM,因为我的 /system/bin 中不存在 chmod。它在 /system/xbin/ - 我正在运行 5.0.8
  • 好吧,我的手机已经变砖了,所以现在我没有手机了。希望我不需要 su?
  • 重新扎根并盗用我的手机 :) 有什么想法吗?

标签: android shell exec root


【解决方案1】:

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

【讨论】:

  • 许多 Android 应用程序都指定需要具有 root 访问权限,并且在 Android 市场上非常成功。在这种情况下,我认为您甚至不需要 su,因为应用程序具有在 /data/data/app-package 目录中运行写入执行的完全访问权限
【解决方案2】:
    executablePath = getIntent().getData().getPath();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多