2013-03-26 53 views
11

我有一个与AsyncTask类有关的概念问题。我们使用AsyncTask,以便主UI不被阻止。 但是,假设我想从设备的内存中检索一些数据,并且我使用AsyncTask类来完成此操作。代码的相关线将如下(假设返回的数据类型的字符串):从AsyncTask中返回数据而不阻塞UI

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

不会上面的行阻止用户界面,击败使用AsyncTask的目的是什么?如果是的话,那么我如何获得相关数据而不会阻塞UI?我想补充一点,下一行代码将需要这些数据来执行一些任务,因此取决于返回的值。

谢谢

+0

AsyncTask解释谷歌:https://developer.android.com/guide/components/processes-and-threads.html#AsyncTask – 2013-03-26 11:09:10

回答

17

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与回调接口像这样

以下是我之前提供的关于相同的答案AsyncTask with Callback

+0

我不想显示该数据,我想要使用该数据。当我搜索我的问题时,我得到了类似的结果。 – Rajat 2013-03-26 11:12:35

+0

@addresseerajat完全阅读答案..最后两行用回调讨论AsyncTask,转到链接一次 – Pragnani 2013-03-26 11:13:31

+0

好吧,我有一些模糊的想法,但我希望将这些数据传递给主UI线程。 – Rajat 2013-03-26 11:21:00

0

当执行一个异步任务,任务经过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线程上通知。