AsynTask - onPostExecute 在 doInBackground 之前调用

发布于 2024-12-29 07:30:51 字数 4499 浏览 1 评论 0原文

我在使 AsyncTask 正常工作时遇到问题。我的应用程序提供了连接您的 Google 帐户并使用任务 API 添加和接收任务的可能性。当用户想要与其帐户同步时,会启动doInBackground()方法。在此之前,ProgressDialog 会显示在 AsyncTask 类的 onPreExecute() 方法中。
如果同步已成功执行,则“应该”调用 onPostExecute() 方法,淡出 ProgressDialog
但有一个问题:在 doInBackground() 中的工作完成之前调用了 onPostExecute() 方法。 在 doInBackground() 中,我收到了用于授权的令牌:

token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

仅此而已。但就在这之前,onPostExecute() 被调用,并且 ProgressDialog 消失,而令牌仍在检索。有线的事情是,当我第一次启动应用程序并同步时,它会正常工作。但此后,onPostExecute() 方法在工作完成之前完成。这是否与执行时向服务器发出请求有关?

future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

如何告诉 onPostExecute() 方法仍有工作要做?

private class SynchronizeGoogle extends AsyncTask<Void, Integer, Void> 
    {
    private final ProgressDialog dialog = new ProgressDialog(RememberMe.this);
    protected void onPreExecute() 
    {
        this.dialog.setMessage("Listen werden geladen...");
        this.dialog.show();
     }

    @Override
    protected Void doInBackground(Void... arg0) 
    {
        try 
            {
            switch(startSynchronize())
                {
                    case 0: 
                        publishProgress(0);
                        return null;
                    case 1: 
                        publishProgress(1);
                        return null;
                    case 2: 
                        publishProgress(2);
                        return null;
                }
            } 
        catch (IOException e) 
            {
                e.printStackTrace();
            }

                    synchronize();


        return null;
    }

    protected void onProgressUpdate(Integer... type)
    {
        int typeCase = type[0];
        switch(typeCase)
        {
            case 0:
                showDialog(DIALOG_INTERNET_ACCESS);
                break;
            case 1:
                showDialog(DIALOG_CREATE_ACCOUNT);
                break;
            case 2:
                showDialog(DIALOG_ACCOUNTS);
                break;

        }


    }

      protected void onPostExecute(final Void unused) 
      {


          if (this.dialog.isShowing()) 
          {
             this.dialog.dismiss();
          }

      }
}

这里是我的startSynchronize()和synchronize()方法:

private int startSynchronize() throws IOException
{
            googleAccountManager = new GoogleAccountManager(RememberMe.this);
    Account[] accounts = googleAccountManager.getAccounts();

    if(checkAccess.internetAccess() == false)
    {
        return 0;
    }
    if(accounts.length == 0)
        {
            return 1;
        }
    else
        {
            if(accounts.length == 1)
            {
                account = accounts[0];

            }
            else
            {
                return 2;
            }
        }

    return -1;
}   

private void synchronize()
{       
myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);

String oldToken = myPrefs.getString(MY_TOKEN, "");

    if(oldToken.length() > 0)
        {
                            // invalidate old token to be able to receive a new one
            googleAccountManager.invalidateAuthToken(oldToken); 

        }

    googleAccountManager.manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, new AccountManagerCallback<Bundle>() 
            {
            public void run(AccountManagerFuture<Bundle> future) 
                    {
                        try 
                            {

                        token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

                                         prefsEditor.putString(MY_TOKEN, token);
                                prefsEditor.commit();

                    useTasksAPI(token);

                            } 
                    catch (OperationCanceledException e) 
                            {
                            //...
                            } 
                    catch (Exception e) 
                            {
                            //...
                            }
                    }
          }, null);


}

在选项菜单中我像这样启动它

new SynchronizeGoogle().execute();

感谢大家的帮助

i got a problem getting my AsyncTask to work correct. My App offers the possibility to connect with your Google Account and add and receive tasks by using the Tasks API. When the users wants to synchronize with his account, the doInBackground() method is started. Right before that, a ProgressDialog is displayed in the onPreExecute() method of the AsyncTask class.
If the synchronisation has been successfully executed, the onPostExecute() method 'should' be called fading out the ProgressDialog.
But there is problem: the onPostExecute() ethod is called before the work in the doInBackground() is finished.
In doInBackground() I receive the token used for the authorization:

token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

That's all. But right before that, the onPostExecute() is called and the ProgressDialog disappears while the token is still retrieving. The wired thing is that when I start the app and synchronizes for the first time, it works like it should. But after that the onPostExecute() method finishes before the work is completed. Does this have to do that there are requests to a server while executing

future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

How can I tell the onPostExecute() method that there is still work to do?

private class SynchronizeGoogle extends AsyncTask<Void, Integer, Void> 
    {
    private final ProgressDialog dialog = new ProgressDialog(RememberMe.this);
    protected void onPreExecute() 
    {
        this.dialog.setMessage("Listen werden geladen...");
        this.dialog.show();
     }

    @Override
    protected Void doInBackground(Void... arg0) 
    {
        try 
            {
            switch(startSynchronize())
                {
                    case 0: 
                        publishProgress(0);
                        return null;
                    case 1: 
                        publishProgress(1);
                        return null;
                    case 2: 
                        publishProgress(2);
                        return null;
                }
            } 
        catch (IOException e) 
            {
                e.printStackTrace();
            }

                    synchronize();


        return null;
    }

    protected void onProgressUpdate(Integer... type)
    {
        int typeCase = type[0];
        switch(typeCase)
        {
            case 0:
                showDialog(DIALOG_INTERNET_ACCESS);
                break;
            case 1:
                showDialog(DIALOG_CREATE_ACCOUNT);
                break;
            case 2:
                showDialog(DIALOG_ACCOUNTS);
                break;

        }


    }

      protected void onPostExecute(final Void unused) 
      {


          if (this.dialog.isShowing()) 
          {
             this.dialog.dismiss();
          }

      }
}

And here my startSynchronize() and synchronize() methods:

private int startSynchronize() throws IOException
{
            googleAccountManager = new GoogleAccountManager(RememberMe.this);
    Account[] accounts = googleAccountManager.getAccounts();

    if(checkAccess.internetAccess() == false)
    {
        return 0;
    }
    if(accounts.length == 0)
        {
            return 1;
        }
    else
        {
            if(accounts.length == 1)
            {
                account = accounts[0];

            }
            else
            {
                return 2;
            }
        }

    return -1;
}   

private void synchronize()
{       
myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);

String oldToken = myPrefs.getString(MY_TOKEN, "");

    if(oldToken.length() > 0)
        {
                            // invalidate old token to be able to receive a new one
            googleAccountManager.invalidateAuthToken(oldToken); 

        }

    googleAccountManager.manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, new AccountManagerCallback<Bundle>() 
            {
            public void run(AccountManagerFuture<Bundle> future) 
                    {
                        try 
                            {

                        token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);

                                         prefsEditor.putString(MY_TOKEN, token);
                                prefsEditor.commit();

                    useTasksAPI(token);

                            } 
                    catch (OperationCanceledException e) 
                            {
                            //...
                            } 
                    catch (Exception e) 
                            {
                            //...
                            }
                    }
          }, null);


}

In the Optionsmenu i start it like this

new SynchronizeGoogle().execute();

Thanks everybody for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2025-01-05 07:30:51

如果我没有误解您的问题,那么您对 ​​getResult() 方法的使用是错误的。

当代码中的任何位置调用 getResult() 时,AsyncTask 不会等到完成。因此,您需要在 onPostExecute 方法中执行您的过程。

我向您推荐这个问题与答案。我希望,它会对你有所帮助。

If I do not misunderstand your question, you're wrong with getResult() method usage.

When getResult() called by anywhere in your code, AsyncTask does not wait until finish. So you need to do your process in onPostExecute method.

I recommend you this question&answer. I hope, it's gonna help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文