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

编写执行linux命令的android应用程序,第1张

概述我有一个已编译的可执行文件,它应该从res文件夹复制到/ data / data / package-name /文件夹中,然后更改权限,然后执行.每一步都完成到最后.输出流似乎是在写等等.除非我去检查文件系统,否则什么都没做.我首先尝试使用’sh’然后使用’su'(我有一个带根的Cyanogen rom). 这是代码: public class UnexecutableActivity exte 我有一个已编译的可执行文件,它应该从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("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!)我将永远感激不尽.

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

以上是内存溢出为你收集整理的编写执行linux命令的android应用程序全部内容,希望文章能够帮你解决编写执行linux命令的android应用程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1019479.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存