Android--手機root獲取與判斷應用是否獲取

import android.util.Log;
 
import java.io.DataOutputStream;
import java.io.File;
 
/** 
 * 判斷手機是否ROOT 
 */  
public class isRoot{
    public static int root() {
    	  
        int root = 0;
  
        try {  
            if ((!new File("/system/bin/su").exists())  
                    && (!new File("/system/xbin/su").exists())) {  
                root = 0;
            } else {  
                root = 1;
            }  
  
        } catch (Exception e) {  
        }  
        return root;  
    }
 
    /**
     *
     * 判斷應用是否有root權限
     */
    public static synchronized boolean getRootAhth()
    {
        Process process = null;
        DataOutputStream os = null;
        try
        {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("exit\n");
            os.flush();
            int exitValue = process.waitFor();
            if (exitValue == 0)
            {
                return true;
            } else
            {
                return false;
            }
        } catch (Exception e)
        {
            Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "
                    + e.getMessage());
            return false;
        } finally
        {
            try
            {
                if (os != null)
                {
                    os.close();
                }
                process.destroy();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

 

發佈了25 篇原創文章 · 獲贊 17 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章