Android 后台任务 - 如何访问 return 值

Android Background Task - how to access return value

当后台任务 return 是一个值时,如何从另一个 class 访问它。仅将此用作示例代码,但我想要的是后台任务做某事和 return 一个值。

protected String doInBackground(String... params) {
    publishProgress("Sleeping..."); // Calls onProgressUpdate()
    try {
       // Do your long operations here and return the result
       int time = Integer.parseInt(params[0]);    

       // Sleeping for given time period
       Thread.sleep(time);
       resp = "Slept for " + time + " milliseconds";
   } catch (InterruptedException e) {
       e.printStackTrace();
       resp = e.getMessage();
   } catch (Exception e) {
       e.printStackTrace();
       resp = e.getMessage();
   }

   **return resp;**
}

为此你需要像

一样扩展 asynktask class
extends AsyncTask<String, String, String>

@Override
protected void onPostExecute(String result) {
    //heare result is value you return from doInBackground() method 
    //this is work on UI thread
} 

类看起来像

public class AsyncTaskGetResult extends AsyncTask<String, String, String> {

    PrintListner mPrintListner ;      
    private AsyncTaskGetResult (PrintListner mPrintListner) {
        this.mPrintListner = mPrintListner;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }
    @Override
    protected String doInBackground(String... params) {
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
    //heare result is value you return from doInBackground() method 
    //this is work on UI thread
    this.mPrintListner.getResult(result);
   }

}

public interface PrintListner {
    public void getResult(String receiptItem);
}

如果您需要在另一个 class 中访问它,您可以为此编写监听器并在您中实现 activity

public class MyActivity extends Activity implements PrintListner{

       @Override
       public void getResult(String receiptItem){
        //Do whatever you want
       }
    }   

并称其为 new AsyncTaskGetResult(this).execute(yourString);

异步任务:

public class YourBackgroundTask extends AsyncTask<String, Void, String> {
     private Callback callback;

     public YourBackgroundTask(Callback callback){
          this.callback = callback;
     }

     protected void doInBackground(String... strings) {
         // do what you have to do
         return result;
     }

     protected void onPostExecute(String result) {
          this.callback.onDone(result);
     }

     public Interface Callback{
          void onDone(String result);
     }
 }

并这样称呼它:

new YourBackgroundTask(yourCallback).execute(yourString);

你有点误会。后台任务没有 return 值。他们只是做点什么。如果你想从后台完成的一些计算中获取一个值,你可以创建后台任务,这样当它完成时,它会通知一些对象关于结果值。

之前的行为可以通过 observer 模式完成,其中一个对象用于 observe 来自另一个对象的事件。在这种情况下,您想定义一个观察者(通常称为 listener)并将其传递给您的后台任务。

您的侦听器界面可能如下所示:

interface ValueListener {
    public void onValueComputed(int computedValue);
}

然后一个实现 class 看起来像这样:

class ValueListenerImpl implements MyListener {
    @Override
    public void onValueComputed(int computedValue) {
        //do something...
    }
}

(或者你可以匿名class)

您的后台任务:

class MyAsyncTask extends AsyncTask<Void, Void, Integer> {
    ValueListener listener;

    public MyAsyncTask(ValueListener valueListener) {
        this.listener = valueListener;
    }

    @Override
    public Integer doInBackground(Void.. params) {
        //do something...
        return someValue;
    }

    @Override
    public onPostExecute(Integer.. values) {
        listener.onValueComputed(values[0]);
    }
}

最后,在你的主线程中你做:

    ...
    this.valueListener = new ValueListenerImpl();
    new MyAsyncTask(this.valueListener).execute();
    ...