2010-08-12 75 views
4

我有一個編譯好的可執行文件,應該將它自己從res文件夾複製到/ data/data/package-name /文件夾中,並更改權限,然後執行。每一步都完成到最後。輸出流似乎在寫,等等。除了當我去檢查文件系統時,什麼都沒有完成。我首先用'sh'和'su'一起嘗試(我有一個根源化的Cyanogen ROM)。編寫執行linux命令的android應用程序

下面是代碼:

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!)我會永遠感激。

+0

我不熟悉pastebin - 你可以做一個鏈接。 – Martin 2010-08-12 17:09:00

+0

爲什麼不把代碼放在這裏? SO支持語法高亮等。 – 2010-08-12 17:13:34

+1

您運行的CM版本是什麼,因爲chmod在我的/ system/bin中不存在。其in/system/xbin/- 即時運行5.0.8 – 2010-08-12 17:17:10

回答

1

不應該使用shell命令。該SDK不包含任何shell命令,並且您不能依賴這些設備上的一致工作。你絕對不能依靠su在各種設備上工作。 :}

+1

大量的Android應用程序指定需要擁有root訪問權限,並且在Android市場上非常成功。在這種情況下,我認爲你甚至不需要su,因爲應用程序可以完全訪問/ data/data/app-package目錄中的執行寫入執行 – 2010-08-12 22:36:41

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

這一行給我一個空指針異常。顯然,getIntent()。getData返回的URI(java.net.Uri)爲null。我正在嘗試解決它,看看我是否可以使用其餘代碼創建該文件。