網頁

2018年10月13日 星期六

[ Android Studio ] 判斷權限ROOT的用法


一、判斷權限ROOT是否為true 
private static boolean checkRootExecutable() {


   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("錯誤:", "錯誤的位置=>" + e.getMessage());
     return false;
   } finally {
     try {
       if (os != null) {
         os.close();
       }
       process.destroy();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
二、放入onStart為載入後顯示對話方塊:
protected void onStart() {
   if(checkRootExecutable()==false){
     AlertDialog.Builder ad=new AlertDialog.Builder(this);
     ad.setTitle("錯誤訊息");
     ad.setMessage("權限未打開(NOT ROOT)");
     ad.setPositiveButton("關閉", new DialogInterface.OnClickListener() {//退出按鈕
       public void onClick(DialogInterface dialog, int i) {
         //不執行
       }
     });
     ad.show();//顯示對話框
   }

   super.onStart();
}

沒有留言:

張貼留言

影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。