Android: How can I pass parameters to AsyncTask's onPreExecute()?
You can override the constructor. Something like:
private class MyAsyncTask extends AsyncTask<Void, Void, Void> { public MyAsyncTask(boolean showLoading) { super(); // do stuff } // doInBackground() et al. }
Then, when calling the task, do something like:
new MyAsyncTask(true).execute(maybe_other_params);Edit: this is more useful than creating member variables because it simplifies the task invocation. Compare the code above with:
MyAsyncTask task = new MyAsyncTask();
task.showLoading = false;
task.execute();referemce:http://stackoverflow.com/questions/3075009/android-how-can-i-pass-parameters-to-asynctasks-onpreexecuteAsyncTask使用方式如下,
activity_main.xml
MainActivity.java
package tw.brad.android.book.MyAsyncTask;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
 private View start, end;
 private TextView mesg;
 private MyMealTime task;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  start = findViewById(R.id.start);
  end = findViewById(R.id.end);
  mesg = (TextView) findViewById(R.id.msg);
  start.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    task = new MyMealTime();
    //傳遞多個參數給AsyncTask運作處理
    task.execute("breakfast", "lunch", "dinner");
   }
  });
  end.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // 提早結束AsyncTask
    if (task != null && !task.isCancelled()) {
     task.cancel(true);
    }
   }
  });
 }
 private class MyMealTime extends AsyncTask < String, Integer, String>{
  private String[] name;
  private boolean isOver;
  @Override
  protected String doInBackground(String... names) {
   name = names;
   for (int i = 0; i < 10; i++) {
    try {
     // 傳遞三個參數處理使用者介面
     publishProgress(i, i + 20, i+40);
     Thread.sleep(1500); // 暫停休眠
     
     // 如果提早結束
     if (isCancelled()) {
      isOver = true;
      break;
     }
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   
   // 提早結束傳會null; 否則正常下傳回結果參數
   return isOver?null:"終於執行結束";
  }
  @Override
  protected void onCancelled() {
   super.onCancelled();
   mesg.setText("提早結束");
  }
  @Override
  protected void onPostExecute(String result) {
   super.onPostExecute(result);
   // 將接收執行結束厚的結果參數顯示在使用者介面
   mesg.setText("Result:" + result);
  }
  @Override
  protected void onPreExecute() {
   super.onPreExecute();
  }
  @Override
  protected void onProgressUpdate(Integer... values) {
   super.onProgressUpdate();
   int i = values[0]; 
   String result="";
   if(name[i%3].equals("breakfast")){
    result=name[i%3] + " at 6:" + values[i%3]+" am";
   }else if(name[i%3].equals("lunch")){
    result=name[i%3] + " at 13:" + values[i%3]+" pm";
   }else if(name[i%3].equals("dinner")){
    result=name[i%3] + " at 19:" + values[i%3]+" pm";
   }
   
   // 將執行過程的傳遞參數顯示在使用者介面
   mesg.setText(result);
  }
 }
} 
沒有留言:
張貼留言