2016-09-15 127 views
1

我在我的android應用程序中運行一個線程時出現了一些問題,它應該顯示一個對話框詢問用戶,如果用戶單擊是,加載對話框應該出現在背景中做某事時,我創建了一個線程,但是當我單擊yes按鈕時,UI仍然鎖定,直到完成該過程。Android線程問題

代碼: 對話框:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

      builder.setMessage("LOGO.bin Was Not Found, Would You Like To Extract It?") 
        .setTitle("LOGO Not Found!"); 

      builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

       getAndExtract(); 


       } 
      }); 
      builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        finish(); 
        System.exit(0); 
       } 
      }); 


      AlertDialog dialog = builder.create(); 
      dialog.show(); 

getAndExtract:

public void getAndExtract() 
{ 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        showLoad("Grabbing Logo..."); 

        getLogo(); 
        Thread.sleep(2000); 
        progressDialog.cancel(); 

        showLoad("Extracting Images..."); 
        extractImages(); 
        Thread.sleep(2000); 
        progressDialog.cancel(); 
       }catch (InterruptedException iE) 
       { 
        iE.printStackTrace(); 
       } 
      } 
     }).run(); 




} 

showLoad:

progressDialog.setMessage(msg); 
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
progressDialog.show(); 

extractImages的基礎:

Command cmd = new Command(0, "LogoInjector -i " + getFilesDir() + "/LOGO.bin -d -g " + getFilesDir() + "/"); 

RootTools.getShell(true).add(cmd); 

基礎getLogo的:

Command cmd = new Command(0, "dd if=/dev/block/mmcblk0p" + partitionIndex + " of=" + getFilesDir() + "/LOGO.bin"); 

RootTools.getShell(true).add(cmd); 

我也試圖把showLoad在runOnUiThread但沒有改變......如果我刪除progressDialog.cancel();它確實顯示了加載對話框,但是在提取已經完成之後。我按是,它只是掛起,直到getLogo()和extractImages()都完成

任何人都可以幫我找出爲什麼這不起作用?

謝謝!

+0

你嘗試使用的AsyncTask()? – ErickBergmann

回答

2

嘗試使用的AsyncTask:

final AsyncTask<Void,Void,Void> asyncTask = new AsyncTask<Void, Void, Void>() { 
     @Override 
     protected Void doInBackground(Void... params) { 

      // do whatever you need to do in background 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 

      // do after finished 
     } 
    }; 
    asyncTask.execute(); 

希望幫助=]