这是代码:
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应用程序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)