2013-02-26 55 views
0

我正在構建一個應用程序,該應用程序必須具有超級用戶權限才能在/ data目錄中寫入修改數據庫文件的內容。我寫一些代碼,獲得root訪問權限,但它給我的日誌不能「獲得root權限或用戶拒絕」給Android應用程序的根訪問權限

我的代碼是

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if(canRunRootCommands()) 
      Toast.makeText(this, "Can Run Root Commands", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public static boolean canRunRootCommands() 
     { 
      boolean retval = false; 
      Process suProcess; 

      try 
      { 
      suProcess = Runtime.getRuntime().exec("su"); 

      DataOutputStream os = new DataOutputStream(suProcess.getOutputStream()); 
      DataInputStream osRes = new DataInputStream(suProcess.getInputStream()); 

      if (null != os && null != osRes) 
      { 
       // Getting the id of the current user to check if this is root 
       os.writeBytes("id\n"); 
       os.flush(); 

       String currUid = osRes.readLine(); 
       boolean exitSu = false; 
       if (null == currUid) 
       { 
        retval = false; 
        exitSu = false; 
        Log.d("ROOT", "Can't get root access or denied by user"); 
       } 
       else if (true == currUid.contains("uid=0")) 
       { 
        retval = true; 
        exitSu = true; 
        Log.d("ROOT", "Root access granted"); 
       } 
       else 
       { 
        retval = false; 
        exitSu = true; 
        Log.d("ROOT", "Root access rejected: " + currUid); 
       } 

       if (exitSu) 
       { 
        os.writeBytes("exit\n"); 
        os.flush(); 
       } 
      } 
      } 
      catch (Exception e) 
      { 
      // Can't get root ! 
      // Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted 

      retval = false; 
      Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage()); 
      } 

      return retval; 
     } 

如何獲得root權限的,這樣我可以更新數據庫文件?

+2

大多數Android設備的根訪問權限受到限制。你必須得到這一點,通常排除設備的保修...... – ppeterka 2013-02-26 10:25:28

+0

意味着我不能根本訪問大多數的Android設備?通過任何方法? – 2013-02-26 10:33:36

+0

沒有。根植的訪問權限僅限於決定根植他們電話(少數)的用戶,並且明確允許您的應用程序根訪問。 – Budius 2013-02-26 10:50:16

回答

0

使用RootTools庫,它使根更容易,您不需要使用Process並使用大量的代碼。

-1

在正常情況下,除非設備已生根,否則根不是您可以實現的。您不能將您的應用程序的FORCE根目錄放在非根目錄條件之外。請研究根意味着什麼。