Android -- 程序判斷手機ROOT狀態,獲取ROOT權限


判斷手機是否具有ROOT權限                                                           

/** 
     * 判斷手機是否ROOT 
     */  
    public boolean isRoot() {  
  
        boolean root = false;  
  
        try {  
            if ((!new File("/system/bin/su").exists())  
                    && (!new File("/system/xbin/su").exists())) {  
                root = false;  
            } else {  
                root = true;  
            }  
  
        } catch (Exception e) {  
        }  
  
        return root;  
    }

上面返回的參數就知道手機是否具有ROOT權限了。

向ROOT權限發送請求信息,以獲取ROOT權限                                    

此方法不涉及底層, 這種方式需要用戶點擊確認才可以獲取.

public class MainActivity extends Activity {  
  
    /** 
     * 創建 
     */  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
  
        // 返回系統包名  
        String apkRoot = "chmod 777 " + getPackageCodePath();  
        RootCommand(apkRoot);  
  
    }  
  
    /** 
     * 應用程序運行命令獲取 Root權限,設備必須已破解(獲得ROOT權限) 
     *  
     * @param command 
     *            命令: String apkRoot="chmod 777 "+getPackageCodePath(); 
     *            RootCommand(apkRoot); 
     * @return 應用程序是/否獲取Root權限 
     */  
    public static boolean RootCommand(String command) {  
  
        Process process = null;  
        DataOutputStream os = null;  
  
        try {  
  
            process = Runtime.getRuntime().exec("su");  
            os = new DataOutputStream(process.getOutputStream());  
            os.writeBytes(command + "\n");  
            os.writeBytes("exit\n");  
            os.flush();  
            process.waitFor();  
  
        } catch (Exception e) {  
            Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());  
            return false;  
  
        } finally {  
  
            try {  
                if (os != null) {  
                    os.close();  
                }  
                process.destroy();  
            } catch (Exception e) {  
            }  
        }  
  
        Log.d("*** DEBUG ***", "Root SUC ");  
        return true;  
  
    }  
  
}

我是天王蓋地虎的分割線                                                                 

 

參考:http://blog.csdn.net/fm9333/article/details/12752415


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM