简体   繁体   English

android asynktask取消不起作用

[英]android asynktask cancelled not working

I'm trying to create a login in my app. 我正在尝试在我的应用中创建一个登录名。

This is my Login activity, where I've created an Asynktask where I'm looking to cancel if the result of getUser() is null. 这是我的登录活动,我在其中创建了一个Asynktask,如果getUser()的结果为null,则希望在其中取消。

public class Login extends AsyncTask<String, Void, Void> {

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

        @Override
        protected Void doInBackground(String... params) {
            ArrayList<String> userValues = new ArrayList<String>();

userValues.add(((EditText)findViewById(R.id.usermail)).getText().toString());

            userValues.add("john");

            if((response = new Database().getUser(userValues)) == "null") {
                cancel(true);
            }

            return null;
        }

        @Override
        protected void onCancelled() {
            Toast.makeText(getApplicationContext(), "Not registered", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onPostExecute(Void result) {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
            super.onPostExecute(result);
        }
    }
}

I wish to cancel the procedure of "login" if the returned value on Database.getUser() returns "null". 如果Database.getUser()上的返回值返回“ null”,我希望取消“登录”过程。 But it seems that is not working and I don't know if I'm either doing something wrong or I'm forgetting something. 但是看来这行不通,我不知道我是在做错什么还是在忘记某件事。

Thank you very much in advance. 提前非常感谢您。

cancel(allowInterrupt)

Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns. 调用此方法将导致doInBackground(Object [])返回后,onCancelled(Object)在UI线程上被调用。 Calling this method guarantees that onPostExecute(Object) is never invoked. 调用此方法可确保永远不会调用onPostExecute(Object)。

That being said, use can use the following approach: 话虽如此,使用可以使用以下方法:

public class Login extends AsyncTask<String, Boolean, Boolean> {

    @Override
    protected Boolean doInBackground(String... params) {
        if (getUser() == null){
             // exit from this doInBackground and proceed to postExecute
             return false;
        }

        // continue

        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result){
           // succes
        }else {
           // cancelled
        }
    }
}

} }

Attempts to cancel execution of this task. 尝试取消执行此任务。 This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. 如果任务已经完成,已经被取消或由于某些其他原因而无法取消,则此尝试将失败。 in my view you can handle the login process on onpost execute as per the result set flag that user can login or not 在我看来,您可以根据用户可以登录或不能登录的结果集标志在onpost执行时处理登录过程

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

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