2010-09-15 126 views
0

我嘗試執行shell命令,但它的工作方式應該如此。即使結果也會回來(就像在LogCat上看到的那樣)。問題在於結果的最後一行。每當最後一行發生readLine()(應該不會發生,temp應該爲空)時,應用程序將永久掛起,並且不會從readLine調用返回。也許你會發現錯誤。我試過readUTF和standart read(),都是同樣的問題。是的,該應用程序獲得了su-rights。執行shell命令的獲取結果失敗/掛起

try 
       { 
        Process process = Runtime.getRuntime().exec("su"); 
        DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
        DataInputStream osRes = new DataInputStream(process.getInputStream()); 
        for (String single : commands) { 
         os.writeBytes(single + "\n"); 
         os.flush(); 
         String temp = new String(); 
         while( (temp = osRes.readLine()) != null) 
         {          
          Log.v("NITRO", temp); 
          result2 += temp + "\n";        
         }      
        } 
        os.writeBytes("exit\n"); 
        os.flush(); 
        process.waitFor(); 
       } catch (IOException e) 
       { 
        Toast.makeText(Main.this, "Error", Toast.LENGTH_LONG); 
       } catch (InterruptedException e) 
       { 
        Toast.makeText(Main.this, "Error", Toast.LENGTH_LONG); 
       } 

這就是它,當我停止調試器掛在掛堆棧跟蹤:

OSFileSystem.readImpl(int, byte[], int, int) line: not available [native method]  
OSFileSystem.read(int, byte[], int, int) line: 118 
ProcessManager$ProcessInputStream(FileInputStream).read(byte[], int, int) line: 312 
ProcessManager$ProcessInputStream(FileInputStream).read() line: 250 
DataInputStream.readLine() line: 309  
Main$2$1.run() line: 84 
ViewRoot(Handler).handleCallback(Message) line: 587 
ViewRoot(Handler).dispatchMessage(Message) line: 92 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 4627  
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
Method.invoke(Object, Object...) line: 521 
ZygoteInit$MethodAndArgsCaller.run() line: 868 
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method] 
+1

在黑暗中拍攝這裏,嘗試移動'退出'之前的'while'循環? – st0le 2010-09-15 10:29:37

+0

您應該回復@ st0le,否則可能永遠無法看到。 – 2010-09-16 00:49:43

+0

@ st0le你是對的,工作,謝謝。從一個例子中抓住了那個命令之後的退出。但我只是想執行一個命令,所以這應該工作。你可以將它發佈爲答案,我可以接受它。 – ludwigm 2010-09-16 16:10:45

回答

0

嘗試while循環之前移動退出...你的應用永遠不會終止,因此while環永遠不會結束....移動exit應該導致應用程序終止,這應該導致循環得到一個null ...