Android - Dialog解雇

时间:2015-03-03 13:25:28

标签: android performance android-fragments android-asynctask android-dialog

我创建了一个简单的登录,用户输入他的详细信息并使用AsyncTask,它将用户输入comapares到SQLite数据库,如果它正确,它将开始主要活动的意图。

问题:

  1. 如果用户密码/用户名不正确,则加载progressDialog不会解除,但如果密码/用户名不正确,则显示else语句吐司
  2. 在OnClick上的Login类中我声明了这里输入的用户的新LoginTask是我的AysncTask

        private static class LoginTask extends AsyncTask<Void, Void, Boolean> {
    
        ProgressDialog pd;
        String username, password;
        private final Context context;
        Intent log;
        // private final WeakReference<Context> reference;
    
        private LoginTask(Context context, String username, String password) {
            this.context = context;
            this.username = username;
            this.password = password;
    
            //final Context context = this.context;
            //Controller handler = new Controller(this.context);
    
            pd = new ProgressDialog(this.context);
    
        }
    
        protected void onPreExecute() {
            //super.onPreExecute();
            pd.show(this.context,"Authenticating account ...", "Please wait ...");
            pd.setCanceledOnTouchOutside(false);
    
        }
    
        @Override
        protected Boolean doInBackground(Void... p) {
            //final Context context = this.context;
            Controller handler = new Controller(this.context);
            handler.open();
            if (!handler.executeLog(username.trim(), password.trim())){
                return false;
            } else {
                return true;
    
            }
        }
    
        @Override
        protected void onPostExecute(Boolean result) {
            pd.dismiss();
           // super.onPostExecute(result);
           // final Context context = this.context;
            Controller handler = new Controller(this.context);
            handler.open();
    
                if (result == false) {
    
                    Toast.makeText(context, "Failed, Incorrect Username/Password", Toast.LENGTH_SHORT).show();
    
                } else {
                    handler.close();
                    Intent log = new Intent(this.context, MainActivity.class);
                    context.startActivity(log);
                    ((Activity)context).finish();
                    Toast.makeText(context, "You have successfully logged on, " + username, Toast.LENGTH_LONG).show();
                }
            }
        }
    

    }

1 个答案:

答案 0 :(得分:1)

改变这个:

protected void onPreExecute() {
      //super.onPreExecute();
       pd.show(this.context,"Authenticating account ...", "Please wait ...");
       pd.setCanceledOnTouchOutside(false);
}

由此:

@Override
protected void onPreExecute() {
    // super.onPreExecute();
    this.pd.setCanceledOnTouchOutside( false );
    this.pd.setCancelable( true );
    this.pd.setTitle( "Authenticating account ..." );
    this.pd.setMessage( "Please wait ..." );
    this.pd.show();

}