简体   繁体   中英

How cancel RequestAsyncTask Facebook SDK 3.0?

Im trying cancel a RequestAsyncTask, when i call the method cancel, background thread doesn't stop, because isCancelled method never is called, how i can do it? here my code:

Request requestUpload = Request.newUploadPhotoRequest(session, imagen, requestCallback);
        Bundle params = requestUpload.getParameters();
        params.putString("name", "hi everyone");
        requestUpload.setParameters(params);
        rq = Request.executeBatchAsync(requestUpload);
        progressDialog = ProgressDialog.show(getActivity(), "", "Wait please...", true, true, new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (rq != null) {
                    rq.cancel(true);                           
                }
            }
        });

A couple of things:

Firstly, I'm not sure how you are able to compile that code, as I'd expect rq to have to be final in order to be referenced in the anonymous OnCancelListener methods. It certainly had to when I reproduced it in my environment.

Secondly, I am not sure what you mean by

because isCancelled method never is called

This method is called to find out if the task was cancelled, not to cancel it.

Thirdly, I wrote similar code (below) and it showed that when the dialog was cancelled using the back button, a call to isCancelled returned true.

Finally, the code as written will show your progress dialog indefinitely, whereas you (presumably) want it to be dismissed when the Request is complete. One way to do that is to make a subclass of the RequestAsyncTask, and override its onPostExecute method to dismiss the ProgressDialog.

Bundle postParams = new Bundle();
postParams.putString("name", "Name");
postParams.putString("message", "Message");
postParams.putString("picture", imageURL);

Request request = new Request(fbSession, "me/feed", postParams, HttpMethod.POST, postResponseCallback);
final FacebookPostTask task = new FacebookPostTask(request);
task.execute();

facebookPostProgressDialog = ProgressDialog.show(this, "", "Posting to Facebook...", true, true,
    new DialogInterface.OnCancelListener() {
      @Override
      public void onCancel(DialogInterface dialog) {
        if (task != null) {
          task.cancel(true);
          Log.d(MainActivity.TAG, "Task cancelled? " + task.isCancelled());
        }
      }
    });

The subclass of the RequestAsyncTask looks like this:

  private class FacebookPostTask extends RequestAsyncTask {

    public FacebookPostTask(Request... requests) {
      super(requests);
    }

    @Override
    protected void onPostExecute(List<Response> result) {
      super.onPostExecute(result);
      facebookPostProgressDialog.dismiss();
    }

  }

There may be slightly simpler ways of doing this.

Note that even when I succeeded in cancelling the task, the FB post seems to have gone ahead. This might just be down to the speed at which I cancelled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM