Android 从AsyncTask返回数据而不阻塞UI

Android 从AsyncTask返回数据而不阻塞UI,android,android-asynctask,Android,Android Asynctask,我有一个与AsyncTask类相关的概念性问题。我们使用AsyncTask,这样主UI就不会被阻塞。 但是假设我想从设备的内存中检索一些数据,我使用AsyncTask类来实现这一点。代码的相关行如下所示(假设返回的数据类型为String): 上面这一行不会阻塞UI,从而破坏使用AsyncTask的目的吗?如果是,那么如何在不阻塞UI的情况下获取相关数据?我想补充一点,下一行代码将需要这些数据来执行某些任务,因此取决于返回的值 谢谢get()方法将阻止UI线程。要获取相关数据,需要从doInBac

我有一个与
AsyncTask
类相关的概念性问题。我们使用
AsyncTask
,这样主UI就不会被阻塞。 但是假设我想从设备的内存中检索一些数据,我使用AsyncTask类来实现这一点。代码的相关行如下所示(假设返回的数据类型为String):

上面这一行不会阻塞UI,从而破坏使用
AsyncTask
的目的吗?如果是,那么如何在不阻塞UI的情况下获取相关数据?我想补充一点,下一行代码将需要这些数据来执行某些任务,因此取决于返回的值

谢谢

get()方法将阻止UI线程
。要获取相关数据,需要从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);
    }
   }
公共类BackgroundTask扩展了AsyncTask{
private ProgressDialog mProgressDialog;
智力进步;
公共背景任务(){
mProgressDialog=新建进度对话框(上下文);
mProgressDialog.setMax(100);
mProgressDialog.setProgress(0);
}
@凌驾
受保护的void onPreExecute(){
mProgressDialog=ProgressDialog.show(上下文“,”加载…”,true,false);
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(整型…值){
setProgress(值[0]);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串数据=getDatafromMemoryCard();
return data;//返回要在此处使用的数据
}
@凌驾
受保护的void onPostExecute(字符串结果){//result是doInBackground返回的数据
Toast.makeText(上下文、结果、Toast.LENGTH_LONG).show();
mProgressDialog.disclose();
super.onPostExecute(结果);
}
}

若您在单独的类中使用asynctask,那个么使用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电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.TextView);
新建任务().execute();
}
类任务扩展异步任务{
受保护的void onPreExecute(){
//显示进度对话框
}
受保护字符串doInBackground(无效…参数){
//做网络运营
回复“你好”;
}
受保护的void onPostExecute(字符串结果){
//关闭对话框//将hello设置为textview
//在这里使用返回值。
tv.setText(result.toString());
}
}
考虑使用robospice(AsyncTask的替代方案)


进行异步调用,在ui线程上发出通知。

AsyncTask由google解释:我不想显示这些数据,我想使用这些数据。我在google搜索我的问题时得到了与您类似的结果。@AddresseRajat完全阅读了答案。.最后两行讨论了关于AsyncTask的回调,再次转到链接OK,我有一些模糊的想法,但我希望将此数据传递到主UI线程。@AddresseRajat回调将向UI线程返回数据。只需使用相同的代码即可。@Alioo如果将此类作为内部类包含,则可以在onCreate中从父类获取上下文。其次,如果将其作为单独的类包含,请为bg TA使用单参数构造函数k、 并从该论点中获取上下文
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.
    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());
 }
 }