2015-04-02 34 views
0

我有一個AsyncTask在doInBackground()部分做了一堆東西,而在這一堆東西之間,我需要等待用戶在我可以繼續之前物理地做一些事情。如何在繼續之前彈出一些對話框讓用戶單擊確定?如何顯示一個對話框以等待用戶在AsyncTask中繼續?

謝謝!

+0

在的AsyncTask結束,你等待用戶輸入? – 2015-04-02 08:36:29

回答

1

在一大堆東西之間,我需要等待用戶 實際做一些事情之前,我可以繼續下去。

你不應該這樣做doInBackground方法,你需要在onPostExecute();與用戶的交互應該在onPostExecute中完成。

你可以在這裏做什麼?

把你的代碼分爲兩部分,完成具有直到在doInBackground後臺用戶交互才能完成的代碼,使用戶交互做,在onPostExecute,之後剩餘的代碼,你可以使用其他的AsyncTask的休息。

0
class LoadData extends AsyncTask<Object, Object, Object> 
    { 

     @Override 
     protected Object doInBackground(Object... p_params) 
     { 
      // Your background code 
      return null; 
     } 


     @Override 
     protected void onPreExecute() 
     { 
      // Display Progress dialog with cancelable false 
      super.onPreExecute(); 
     } 
     @Override 
     protected void onPostExecute(Object p_result) 
     { 
      // Dismiss Progress dialog 
      super.onPostExecute(p_result); 
     } 
    } 
0

如果你想要把等待對話框中doInBackground段之間,那麼你可以試試下面的代碼:

@Override 
    protected Void doInBackground(Void... params) { 
     activity.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       final Dialog dialog = new Dialog(activity); 
       dialog.setTitle("Demo"); 
       Button button = new Button(activity); 
       button.setText("Press For Process.."); 
       dialog.setContentView(button); 
       button.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         Toast.makeText(activity, "Perform Task", 
           Toast.LENGTH_LONG).show(); 
         // You can perform task whatever want to do after 
         // on user press the button 
         dialog.dismiss(); 
        } 
       }); 

       dialog.show(); 
      } 
     }); 
     return null; 
    } 
+0

這裏所有答案中最好的!應該被接受 – GOLDEE 2015-04-02 09:15:31

相關問題