2011-10-11 189 views
1

任何人都知道如何去做到這一點? 這是我的代碼:以root用戶身份運行本機android可執行文件?

try { 
      Process p = Runtime.getRuntime().exec("su"); 
      p.waitFor(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      Process p = Runtime.getRuntime().exec(
        "/system/bin/netcfg > /sdcard/netcfg.txt"); 
      p.waitFor(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
} 

是的,我知道我還沒有得到任何測試,看看用戶實際接受了超級用戶對話,但是這僅僅是一個測試自己。

+0

你自己測試一下嗎? – MasterCassim

+0

@MasterCassim我自己測試過,我在問怎麼做,因爲它不工作。 –

回答

-1

我從來沒有嘗試過自己,但它wouldnt使很多的感覺讓你能夠從一個應用程序中做的東西,蘇不蘇權利。所以,我想你需要做的第一件事就是爲你的應用程序的請求蘇權利:

// arbitrary number of your choosing 
final int SUPERUSER_REQUEST = 2323; 

// superuser request 
Intent intent = new Intent("android.intent.action.superuser"); 

// tell Superuser the name of the requesting app 
intent.putExtra("name", "Shell"); 

// tel Superuser the name of the requesting package 
intent.putExtra("packagename", "koushikdutta.shell"); 

// make the request! 
startActivityForResult(intent, SUPERUSER_REQUEST); 

然後,您可以:

Runtime.getRuntime().exec("su -c <your privileged command here>"); 

(從http://forum.xda-developers.com/showpost.php?p=2954887&postcount=7複製)

+0

我會嘗試,謝謝:-) –

+0

您的文章+ http ://matejcik.blogspot.com/2010/10/how-to-correctly-execute-su-from.html爲我工作;) –

+0

錯誤。應用程序可以執行「su」,此時超級用戶應用程序要求用戶有權以超級用戶模式運行應用程序的命令。無需自己運行超級用戶活動。 –

0

經過測試,它不適用於Nexus S OS 2.3.6。

更新

,因爲你的手機是植根su應該工作。您看到的問題是密碼對話框不顯示。這似乎適用於某些固定電話,但可能是自定義固件的一項功能。

你可以做的是啓動su進程,通過對話框詢問用戶密碼,然後輸出到su進程。

Process process = new ProcessBuilder() 
    .command("su", "android.com") 
    .redirectErrorStream(true) 
    .start(); 

// Dialog for asking pass here 
OutputStream out = process.getOutputStream(); 
out.write(password); 
+0

我知道,這就是爲什麼我問如何去做。 –

+0

AFAIK你不能(在普通的非根深蒂固的手機上)。這將違反安全原則:http://developer.android.com/guide/topics/security/security.html –

+1

我知道,我紮根;) –

1

這會不會是你的完整解決方案,但這是我剛剛嘗試的一些代碼。它可以運行任意命令(代碼在這裏使用ls)。從屏幕截圖中可以看到,超級用戶對話框會彈出ls

enter image description here

我原本一直在努力做的是打開外殼,sh然後該殼中運行命令,以便將每一個顯示只有初始sh超級用戶對話框。我看過似乎這樣做的應用,但我仍然不確定它是如何完成的。我懷疑這也是你在尋找的,但至少在這個代碼中你可以運行命令。 :)

無論如何,這是代碼。將其粘貼到一個骷髏Android應用程序,它應該工作。

public class ShellCommandTestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final StringBuilder log = new StringBuilder(); 
    try{ 
     ArrayList<String> commandLine = new ArrayList<String>(); 
     commandLine.add("su"); 
     commandLine.add("-c"); 
     commandLine.add("ls"); 

     Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0])); 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

     for (String command : commandLine){ 
      Log.i("SU_Test", command); 
     } 

     String line; 
     while ((line = bufferedReader.readLine()) != null){ 
      log.append(line); 
      log.append("\n"); 
     } 
    } 
    catch (IOException e){ 
     Log.e("SU_Test", "Fail: " + e.getMessage()); 
    } 
    Log.i("SU_Test", log.toString()); 
} 

祝你好運!

+0

真棒,謝謝!將嘗試;) –

相關問題