简体   繁体   English

如何取消RequestAsyncTask Facebook SDK 3.0?

[英]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? 我正在尝试取消RequestAsyncTask,当我调用方法cancel时,后台线程不会停止,因为从未调用过isCancelled方法,我该怎么办? 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. 首先,我不确定您如何编译该代码,因为我希望rq必须是最终的才能在匿名OnCancelListener方法中被引用。 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 因为从不调用isCancelled方法

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. 第三,我写了类似的代码(如下),它表明当使用后退按钮取消对话框时,对isCancelled的调用返回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. 一种实现方法是制作RequestAsyncTask的子类,并重写其onPostExecute方法以关闭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: RequestAsyncTask的子类如下所示:

  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. 请注意,即使我成功取消了任务,FB帖子似乎也已经进行了。 This might just be down to the speed at which I cancelled. 这可能只是我取消的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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