2014-12-05 74 views
0
  • 我有我的根深蒂固仿真器(雖然我不知道它的 相關)Android的 - 不能在模擬器執行簡單的CD

  • 我試圖建立一個根的應用程序爲我的當按下按鈕時,它試圖找到一個數據庫文件。

  • 我已經到了要嘗試從應用程序(java代碼)中的 執行shell命令的地步,但我所能做的只是ls

  • 當我試圖cd到另一個文件夾,它不起作用。在執行cd 後,我執行pwdls,其仍給出/的結果。

  • 下面是

    Button btn_read = (Button) findViewById(R.id.button); 
        btn_read.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          /*String s; 
    
          try { 
           Process p = Runtime.getRuntime().exec("ls -al"); 
           p = Runtime.getRuntime().exec("adb shell"); 
           p = Runtime.getRuntime().exec("cd data/data"); 
    
           BufferedReader stdInput = new BufferedReader(new 
             InputStreamReader(p.getInputStream())); 
    
           BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(p.getErrorStream())); 
    
           // read the output from the command 
           Log.d("ReadDB", "Here is the standard output of the command:\n"); 
           while ((s = stdInput.readLine()) != null) { 
            Log.d("ReadDB", s); 
           } 
    
           // read any errors from the attempted command 
           Log.d("ReadDB", "Here is the standard error of the command (if any):\n"); 
           while ((s = stdError.readLine()) != null) { 
            Log.d("ReadDB", s); 
           } 
    
           System.exit(0); 
          } 
          catch (IOException e) { 
           Log.d("ReadDB", "Exception happened - here's what I know: "); 
           e.printStackTrace(); 
           System.exit(-1); 
          }*/ 
    
          executeCommand(new String[] { 
           "sh", "-c", "ls -l" 
          }); 
          /*executeCommand(new String[] { 
           "sh", "-c", "cd /mnt" 
          });*/ 
          executeCommand(new String[] { 
           "sh", "-c", "ls -l data/data" 
          }); 
          executeCommand(new String[] { 
           "sh", "-c", "pwd" 
          }); 
         } 
        }); 
    } 
    
    private static void executeCommand(String[] commands) { 
        Process p; 
        try { 
         p = Runtime.getRuntime().exec(commands); 
         p.waitFor(); 
         BufferedReader reader = 
           new BufferedReader(new InputStreamReader(p.getInputStream())); 
    
         String line = ""; 
         while ((line = reader.readLine())!= null) { 
          Log.d("ReadDB", line + "\n"); 
         } 
         Log.d("ReadDB", "--------------------------------------------------------------------------------------"); 
    
        } catch (Exception e) { 
         Log.d("ReadDB", "Exception occured"); 
         e.printStackTrace(); 
        } 
    } 
    

誰能幫助我的代碼的相關部分?

+0

謝謝!它似乎在工作 – Warmaster 2014-12-05 13:54:18

回答

1

該命令將運行在單獨的subshells,所以這是不可能的。什麼應該工作:

executeCommand(new String[] { 
    "sh", "-c", "cd /mnt && some other command" 
});