AsynTask-onPostExecute在doInBackground之前被调用 [英] AsynTask - onPostExecute is called before doInBackground

查看:67
本文介绍了AsynTask-onPostExecute在doInBackground之前被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

仅此而已.但是在此之前,调用onPostExecute()ProgressDialog消失,而令牌仍在检索中.有线的事情是,当我启动应用程序并首次进行同步时,它的工作方式应该像它应该的那样.但是之后,onPostExecute()方法完成,然后工作完成.这样做是不是在执行

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);

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

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();
          }

      }
}

这是我的startSynchronize()和syncnize()方法:

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();

感谢大家的帮助

推荐答案

如果我不误解您的问题,那么getResult()方法的用法是错误的.

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

在代码中的任何地方调用getResult()时,AsyncTask都不会等到完成.因此,您需要使用onPostExecute方法进行处理.

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.

这篇关于AsynTask-onPostExecute在doInBackground之前被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆