当后台任务返回值时,如何从另一个类访问它。只是使用它作为示例代码,但我想要的是执行某些操作并返回值的后台任务。
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;**
}
答案 0 :(得分:1)
为此你需要像
那样扩展asynktask类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);
}
如果您需要在另一个类中访问它,您可以为其编写listner并在您的活动中实现
public class MyActivity extends Activity implements PrintListner{
@Override
public void getResult(String receiptItem){
//Do whatever you want
}
}
并将其称为new AsyncTaskGetResult(this).execute(yourString);
答案 1 :(得分:0)
的AsyncTask:
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);
答案 2 :(得分:0)
你有一点误会。后台任务不返回值。他们只是做点什么。如果你想从后台完成的某些计算得到一个值,你可以做后台任务,这样当它完成时,它会通知某个对象有关结果值。
以前的行为可以通过 observer 模式完成,其中一个对象用于观察来自另一个对象的事件。在这种情况下,您需要定义一个观察者(通常称为侦听器)并将其传递给后台任务。
您的侦听器界面可能如下所示:
interface ValueListener {
public void onValueComputed(int computedValue);
}
然后实现类看起来像这样:
class ValueListenerImpl implements MyListener {
@Override
public void onValueComputed(int computedValue) {
//do something...
}
}
(或者您可以制作匿名课程)
您的后台任务:
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();
...