2013-03-16 62 views
0

我有以下AsyncTask暫停的AsyncTask執行,基於用戶輸入提示用戶輸入並繼續執行

public class LoadFromCloudTask extends AsyncTask<Void, String, Boolean> { 

    public LoadFromCloudTask(LoadFromCloudTaskFragment loadFromCloudTaskFragment) { 
     this.loadFromCloudTaskFragment = loadFromCloudTaskFragment; 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) { 
     ... 
     ... 
     ... 
     // How possible I can halt my execution, prompt for user input, and continue execution based on user input. 
     // In Swing, I can ... 
     // 
     /* 
      // Hold the execution, and prompt for user input 
      final int result = JOptionPane.showConfirmDialog(LoadFromCloudJDialog.this, message, MessagesBundle.getString("question_title_overwrite_your_data_by_cloud_data_at"), JOptionPane.YES_NO_OPTION); 
      // Continue execution based on user input. 
     */ 
    } 
} 

我想知道,是否有可能停止的AsyncTask執行,提示用戶輸入並繼續基於執行在用戶輸入?

在Swing中,我可以通過簡單地使用JOptionPane.showConfirmDialog來實現此目的。 Android如何?

回答

1

由於doInBackground執行在不同的線程,你必須暫停這個線程,同時顯示在UI線程一些dialog,一旦該對話框駁回你可以恢復AsyncTask和讀取結果。

1

您可以通過使用publishProgress從內發送消息到UIThread實現這一目標並使用java的等待/通知 mechanisem等待untel用戶輸入他的輸入。

一個psudo代碼會是這個樣子:

public Object mLock = new Object(); 
protected void doInBackground(Params... params){ 
    /// do your requests. 
    publishProgress(Progress ...progress); 
    mLock.wait(); 

} 
protected void onProgressUpdate(Progress ....progress) { 
    // request user input here 
} 

,並從UIThread只是叫mLock.notify()問碼繼續。

您也可以通過拆分跨多個AsynchTasks你的邏輯執行slimier的行爲,因爲AsynchTasks通過共同的(靜態)的ThreadPoolExecutor管理和

1
You can create pause and resume method inside asyntask as below, and call it according to your convenience. 

     public class LoadFromCloudTask extends AsyncTask<Void, String, Boolean> { 

      public LoadFromCloudTask(LoadFromCloudTaskFragment loadFromCloudTaskFragment) { 
       this.loadFromCloudTaskFragment = loadFromCloudTaskFragment; 
      } 

    //call this when you want to halt the task   
        public void pause() { 
          if (this.getStatus().equals(Status.RUNNING)) { 
            try { 
              this.wait(); 
            } catch (InterruptedException e) { 
              e.printStackTrace(); 
            } 
          } 
        } 

    // call this when you need to resume the task 
        public void resume() { 
          if (this.getStatus().equals(Status.PENDING)) { 
            this.notify(); 
          } 
        } 

      @Override 
      protected Boolean doInBackground(Void... params) { 
       ... 
       ... 
       ... 
       // How possible I can halt my execution, prompt for user input, and continue execution based on user input. 
       // In Swing, I can ... 
       // 
       /* 
        // Hold the execution, and prompt for user input 
        final int result = JOptionPane.showConfirmDialog(LoadFromCloudJDialog.this, message, MessagesBundle.getString("question_title_overwrite_your_data_by_cloud_data_at"), JOptionPane.YES_NO_OPTION); 
        // Continue execution based on user input. 
       */ 
      } 
     } 
+0

已經爲這一做法寄予厚望它不會是非常昂貴的,但沒有工作。在暫停()上崩潰。 :( – 2018-01-25 00:25:41

相關問題