Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> Android應用程序請求root訪問

Android應用程序請求root訪問

編輯:中級開發

前提是手機已經破解獲取root權限
This snippet shows how root access can be requested inside an application in order to write a file into a place we do not have permission to access usually. Requesting root Access will only work if your phone allows it, or it has been ‘rooted’ (hacked to allow superuser permissions).

  1. Process p;
     
  2. try {
     
  3.    // Preform su to get root privledges
     
  4.    p = Runtime.getRuntime().exec("su"); 
     

  5.  
  6.    // Attempt to write a file to a root-only
     
  7.    DataOutputStream os = new DataOutputStream(p.getOutputStream());
     
  8.    os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");
     

  9.  
  10.    // Close the terminal
     
  11.    os.writeBytes("exit\n");
     
  12.    os.flush();
     
  13.    try {
     
  14.       p.waitFor();
     
  15.            if (p.exitValue() != 255) {
     
  16.                   // TODO Code to run on success
     
  17.               toastMessage("root");
     
  18.            }
     
  19.            else {
     
  20.                    // TODO Code to run on unsuccessful
     
  21.                    toastMessage("not root");
     
  22.            }
     
  23.    } catch (InterruptedException e) {
     
  24.       // TODO Code to run in interrupted exception
     
  25.            toastMessage("not root");
     
  26.    }
     
  27. } catch (IOException e) {
     
  28.    // TODO Code to run in input/output exception
     
  29.         toastMessage("not root");
     
  30. }

Where my “toastMessage” is just a function which creates a toast to display on the screen. On phones with superuser permissions installed (root access) this will display a dialog asking the user to allow or deny the application permission to have root Access

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved