簡體   English   中英

從AsyncTask返回數據而不阻止UI

[英]Returning data from AsyncTask without blocking UI

我有一個與AsyncTask類相關的概念問題。 我們使用AsyncTask因此不會阻止主UI。 但是假設,我想從設備的內存中檢索一些數據,為此我使用AsyncTask類。 代碼的相關行如下(假設返回的數據類型是String):

  //code
    String data = new ExtendedAsyncTask().execute(param1, param2).get();
  //use this returned value.

上面的行不會阻止UI,打敗使用AsyncTask的目的嗎? 如果是,那么如何在不阻止UI的情況下獲取相關數據? 我想補充一點,下一行代碼將需要這些數據來執行某些任務,因此取決於返回的值。

謝謝

get() method will block the UI thread 要獲取相關數據,您需要從doInBackground返回值並捕獲onPostExecute參數中的值。

doInBackground返回的值由onPostExecute方法捕獲

例:

public class BackgroundTask extends AsyncTask<String, Integer, String >{
       private ProgressDialog mProgressDialog;
       int progress;
       public BackgroundTask() {
           mProgressDialog = new ProgressDialog(context);
             mProgressDialog.setMax(100);
             mProgressDialog.setProgress(0);
    }

       @Override
    protected void onPreExecute() {
           mProgressDialog =ProgressDialog.show(context, "", "Loading...",true,false);
        super.onPreExecute();
    }
     @Override
     protected void onProgressUpdate(Integer... values) {
     setProgress(values[0]);
  }

    @Override
    protected String doInBackground(String... params) {
            String data=getDatafromMemoryCard();    

        return data;  // return data you want to use here
    }
    @Override
    protected void onPostExecute(String  result) {  // result is data returned by doInBackground
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
        mProgressDialog.dismiss();
        super.onPostExecute(result);
    }
   }

如果你在單獨的類中使用asynctask,那么像這樣使用帶有回調接口的AsyncTask

以下是我之前提供的關於使用Callback的相同AsyncTask的答案

你沒有這樣得到結果。 請參閱此鏈接以獲取示例: https//github.com/levinotik/ReusableAsyncTask/tree/master/src/com/example

基本上,這是你需要做的:

  • 定義您的活動實現的接口(=偵聽器)
  • 在asynctask中設置監聽器
  • 在onPostExecute中調用yourListener.yourMethod()

執行異步任務時,任務將執行4個步驟:

1.onPreExecute(), invoked on the UI thread before the task is executed. use this to diaply progress dialog.

2.doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. Can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

3.onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). Used to publish progress.

4.onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

在你的活動onCreate()

    TextView tv;
   @Override
   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);
    tv= (TextView)findViewById(R.id.textView);
    new TheTask().execute();

  }
class TheTask extends AsyncTask<Void, Void,String> {

  protected void onPreExecute() {
  //dispaly progress dialog
 }

 protected String doInBackground(Void... params) {
   //do network operation
     return "hello"; 
 }

 protected void onPostExecute(String result) {  
  //dismiss dialog. //set hello to textview
      //use the returned value here.
     tv.setText(result.toString());
 }
 }

考慮使用robospice(到的AsyncTask的替代品。 https://github.com/octo-online/robospice

進行異步調用,在ui線程上通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM