onPostExecute在doInBackground之前执行

时间:2014-01-27 18:14:23

标签: android-asynctask

我遇到了以下问题。我试着寻找答案,但没有一个回复可以帮助我,所以我在这里问这个问题。任何回复将不胜感激!

我在AsyncTask下面使用它来使用Firebase API公开的登录方法。但是当我在点击登录按钮时调用new LoginOperation()。execute()时,我没有看到预期的结果。我粘贴下面的代码和Logcat输出。

我想知道为什么onPostExecute在doInBackground之前被执行了?请注意我使用的是有效的电子邮件ID&密码,所以我应该能够正确登录。

代码:

private class LoginOperation extends AsyncTask<Void, Void, Boolean> {


    protected Boolean doInBackground(Void... params) {
        try{
            authClient.loginWithEmail(emailid.getText().toString(),password.getText().toString(), new SimpleLoginAuthenticatedHandler() {
                public void authenticated(
                        com.firebase.simplelogin.enums.Error error, User user) {
                     if(error != null) {
                          // There was an error logging into this account
                          loginStatus=false;
                          errorMsg=error.name();
                          Log.d(appName, "Inside if block  in doInBackground of LoginOperation");
                        }
                        else {
                          // We are now logged in
                            loginStatus=true;
                            Log.d(appName, "Inside else block  in doInBackground of LoginOperation");
                        }

                }
                });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return Boolean.valueOf(loginStatus);
    }


    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (result.booleanValue()) {
            toastMsg="User logged in successfully";               
            Log.d(appName, "Inside onPostExecute  success of LoginOperation");

          }
        else
        {
           toastMsg="Error in login";
           Log.d(appName, "Inside onPostExecute failure of LoginOperation");

        }

         TextView displayStatus = (TextView) findViewById(R.id.displayStatus);
         displayStatus.setText(toastMsg); 

    }


    protected void onPreExecute() {
        super.onPreExecute();
    }


    protected void onProgressUpdate(Void... values) {}
}

登录时调用的代码点击:

public void onLogin(View arg0) 
{
Log.d(appName, " email id is " + emailid.getText().toString());
Log.d(appName, " password is " + password.getText().toString());

try {
 Boolean finalStatus= new LoginOperation().execute().get(5000, TimeUnit.MILLISECONDS);
 Log.d(appName, " final Status is: " + finalStatus.booleanValue());
 } 

logcat的:

01-27 17:50:02.054: D/LOGIN(984):  email id is abc@gmail.com
01-27 17:50:02.054: D/LOGIN(984):  password is abc123
01-27 17:50:02.082: D/LOGIN(984):  final Status is: false
01-27 17:50:02.082: D/LOGIN(984): Inside onPostExecute failure of LoginOperation
01-27 17:50:05.502: D/LOGIN(984): Inside else block  in doInBackground of LoginOperation

预期结果:

Inside else block  in doInBackground of LoginOperation
Inside onPostExecute  success of LoginOperation

3 个答案:

答案 0 :(得分:1)

这很可能是因为SimpleLoginAuthenticatedHandler.authenticated()是异步执行的。看起来你根本不需要AsyncTask。

答案 1 :(得分:1)

我的猜测是loginWithEmail()Thread内运行。这意味着当你调用这个方法时,线程确实在运行,但这并不意味着它已经结束了它的执行,所以也许它正在运行,你回来了,完成doInBackground()方法并加入{{ 1}}方法。

我会删除onPostExecute()如果是这样的话,就没有必要了。

答案 2 :(得分:1)

您不需要AsyncTask,因为您在doInBackground中使用的类似乎已经在后台线程中执行了它的任务。将doInBackground中的代码移动到UI线程,并将代码从onPostExecute移到SimpleLoginAuthenticationHandler

目前,onPostExecuted会立即被调用,因为它只是启动一个新的后台线程并立即返回。