2015-09-11 30 views
0

我是一名初學者的android開發人員,在製作示例應用程序時嘗試學習東西。應用程序佈局不會繪製,直到根權限被授予拒絕

我的MainActivity的onCreate()執行一個AsyncTask,爲我的RecyclerView抓取數據。 AsyncTask需要Root訪問並「請求」它。

我的問題是,MainActivity的佈局不會繪製,直到根提示出現,我點擊授予\拒絕。

我怎樣才能使佈局預先繪製?

在此先感謝!

的onCreate():

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
... 
DataFetcher dataFetcher = new DataFetcher(); 
dataFetcher.execute(""); 

dataFetcher:

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     if(!canRunRootCommands()) { 
      Log.e("DataFetcher", "No Root Access"); 
      cancel(true); 
     } 
    } 
/***********************************************************************/ 
    //Root Check method 
    //Credit: http://muzikant-android.blogspot.co.il/2011/02/how-to-get-root-access-and-execute.html 

    /***********************************************************************/ 
    private 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; 
    } 
+0

試圖將AsyncTask調用移動到菜單,該菜單凍結了用戶界面直到我授予\拒絕根權限。 我認爲主線程凍結,直到我這樣做,但爲什麼?因爲它發生在一個AsyncTask中 – ND88

回答

0

移動的 「canRunRootCommands()」 從onPreExecute調用doInBackground,以確保其不會在UI線程上運行。