加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 23:21:28 所属栏目:Linux 来源:网络整理
导读:我有一个已编译的可执行文件,它应该从res文件夹复制到/ data / data / package-name /文件夹中,然后更改权限,然后执行.每一步都完成到最后.输出流似乎是在写等等.除非我去检查文件系统,否则什么都没做.我首先尝试使用’sh’然后使用’su'(我有一个带根的Cyan
我有一个已编译的可执行文件,它应该从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,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("exitn");
            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("exitn");
            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!)我将永远感激不尽.

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读