6
public class ListingFoundBeaconService 
                    extends AsyncTask<String, String, String> {

    public ListingFoundBeaconService(Context contextGiven, 
                                     JSONObject jsonParams) {
        this.contextGiven = contextGiven;
        this.jsonParams = jsonParams;
    }

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(contextGiven);
        pDialog.setMessage("Loading list of active Beacons..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        Log.d("onPreExecute","onPreExecute worked" );
    }

    protected String doInBackground(String... args) {}

    protected void onPostExecute(String file_url) {   
        // Two activities will need this thread so I have 
        // kept this as a separate class. Here I want to send a
        // boolean value to the parent activity to show that 
        // the task has completed or not.
    }

我可以在onPostExecute()函数中触发通知或完成事件侦听器,以便通知启动此类(ListingFoundBeaconService)的父类吗?这样做的标准方法是什么?

4

2 回答 2

19

最好的方法是调用委托。在你的AsyncTask类中创建一个构造函数并有一个委托

代表界面:

public interface TaskDelegate {
    public void taskCompletionResult(String result);
}

现在在AsyncTask

private TaskDelegate delegate;

public ListingFoundBeaconService(Context contextGiven, 
                                 JSONObject jsonParams,
                                 TaskDelegate delegate) {
    this.contextGiven = contextGiven;
    this.jsonParams = jsonParams;
    this.delegate = delegate;
}

postExecute

delegate.taskCompletionResult(result/msg/json);

在您的主类中实现TaskDelegate并实现一个在任务完成时调用的方法。

于 2012-11-16T12:38:19.400 回答
0

将 ListingFoundBeaconService 类抽象化,然后在调用类中重写 onPostExecute。

于 2017-03-08T23:00:21.107 回答