简体   繁体   English

如何将结果从asynctask返回到Android中的活动?

[英]How to return the result from the asynctask to an activity in android?

I have created a Login activity which uses another class - LoginService which is an AsyncTask for the network communication. 我创建了一个Login活动,该活动使用另一个类-LoginService,这是用于网络通信的AsyncTask。

 public void onClick(View view) {

            if (editTextPassword.getText().toString() != null & editTextUsername.getText().toString() != null){

                new LoginService(editTextUsername.getText().toString(), editTextPassword.getText().toString()).execute();

                if(loginSuccess!=false){
                //Used to move to the Cases Activity
                Intent casesActivity = new Intent(getApplicationContext(), CasesActivity.class);
                startActivity(casesActivity);
                }else{
                    Toast.makeText(getApplicationContext(),"Incorrect Details", Toast.LENGTH_LONG).show();
                }
             }
            else{
                //Display Toaster for error
                Toast.makeText(getApplicationContext(),"Please enter your details", Toast.LENGTH_LONG).show();
            }
        }

Before the LoginService has finished executing, the activity has already moved to another activity via the Intent variable. 在LoginService完成执行之前,活动已通过Intent变量移至另一个活动。 I do not understand why. 我不理解为什么。 The idea of the LoginService is to validate the credentials of the user. LoginService的想法是验证用户的凭据。 If it returns true, then it can switch to the other activity. 如果返回true,则可以切换到其他活动。

You do not want to do this in this way. 您不想以这种方式这样做。 The .execute() will begin as soon as possible, but there is no guarantee (and perhaps guaranteed not to) that it will get your loginSuccess value back to you in time. .execute()将尽快开始,但不能保证(也许不能保证)它会及时将您的loginSuccess值返回给您。

Everything after new LoginService(...).execute(); new LoginService(...).execute(); should be moved into onPostExecute() : 应该移到onPostExecute()

private Context mContext = null;
public void setContext(Context context) {
    mContext = context;
}
@Override
protected void onPostExecute(Void result) {
    if(loginSuccess!=false){
        //Used to move to the Cases Activity
        Intent casesActivity = new Intent(mContext, CasesActivity.class);
        startActivity(casesActivity);
    }else{
        Toast.makeText(mContext,"Incorrect Details", Toast.LENGTH_LONG).show();
    }
}

Then, you have to call setContext() like so: 然后,您必须像这样调用setContext()

LoginService service = new LoginService(editTextUsername.getText().toString(), editTextPassword.getText().toString());
service.setContext(getApplicationContext());
service.execute();

You should move 你应该搬家

Intent casesActivity = new Intent(getApplicationContext(), CasesActivity.class);
            startActivity(casesActivity);
            }else{
                Toast.makeText(getApplicationContext(),"Incorrect Details",     Toast.LENGTH_LONG).show();
            }

into LoginService's onPostExecute. 进入LoginService的onPostExecute。

In this way you are sure the asynctask has finished its work. 这样,您可以确保asynctask已完成其工作。

In any case it's quite strange the other activity gets started, it might be because of an old assignement of loginSuccess to true 无论如何,其他活动开始很奇怪,这可能是由于将loginSuccess分配给true的原因

How to return the result from the asynctask? 如何从asynctask返回结果?

Catch the result of AsyncTask from onPostExecute() . onPostExecute()捕获AsyncTask的结果。

@Override
public void onPostExecute(Boolean result)
{
    boolean loginSuccess = result;

    if(loginSuccess!=false) { 

         Intent casesActivity = new Intent(getApplicationContext(), CasesActivity.class);
         startActivity(casesActivity);
    }
    else {

        Toast.makeText(getApplicationContext(),"Incorrect Details", Toast.LENGTH_LONG).show();
    }

}

The data type of result in AsyncTask depends on the 3rd Type parameter. AsyncTask中结果的数据类型取决于3rd Type参数。

Sometimes we think execute() method of AsyncTask will return a result which is wrong. 有时我们认为AsyncTask execute()方法将返回错误的结果。 It will return an AsyncTask itself 它将自己返回一个AsyncTask

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

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