如何使主线程等待AsyncTask android,而不阻止UI

时间:2015-04-21 06:17:23

标签: android multithreading android-asynctask

我正在尝试创建登录视图。 我' d想要启动一个新的AsyncTask来执行对服务器的REST调用并显示progress bar。我需要UI main thread不会阻止,它必须显示带有消息的祝酒(如成功或失败),具体取决于AsyncTask返回的内容。

这里是代码:

SetupActivity(主线程):

//Get reference SignUp Button
    Button signupButton = (Button)myDialog.findViewById(R.id.button_signup_OK);
    signupButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Get all the textfield content from the form
            name=((EditText)myDialog.findViewById(R.id.nameEditText)).getText();
            surname=((EditText)myDialog.findViewById(R.id.surnameEditText)).getText();
            email=((EditText)myDialog.findViewById(R.id.emailEditText)).getText();
            password=((EditText)myDialog.findViewById(R.id.passwordEditText)).getText();
            password_Retyped=((EditText)myDialog.findViewById(R.id.passwordRepEditText)).getText();

            //Get hash from password
            hashPassword=DigestMd5.md5(password);
            hashPasswordRep=DigestMd5.md5(password_Retyped);

            //Check if the fields are null
            if(name.toString().equals("")){
                ((EditText) myDialog.findViewById(R.id.nameEditText)).setError(getString(R.string.mandatoryField));
            }
            if(surname.toString().equals("")){
                ((EditText) myDialog.findViewById(R.id.surnameEditText)).setError(getString(R.string.mandatoryField));
            }
            if(email.toString().equals("") ){
                ((EditText) myDialog.findViewById(R.id.emailEditText)).setError(getString(R.string.mandatoryField));
            }else{
                if(!new EmailValidator().validate(email.toString())){
                    ((EditText)myDialog.findViewById(R.id.emailEditText)).setError(getString(R.string.emailWrong));
                }
            }
            if(password.toString().equals("")){
                ((EditText) myDialog.findViewById(R.id.passwordEditText)).setError(getString(R.string.mandatoryField));
            }
            if(password_Retyped.toString().equals("")){
                ((EditText) myDialog.findViewById(R.id.passwordRepEditText)).setError(getString(R.string.mandatoryField));
            }

            //Check match password
            if(!hashPassword.equals(hashPasswordRep)){
                ((EditText)myDialog.findViewById(R.id.passwordEditText)).setError(getString(R.string.passwordNotMatching));
                ((EditText)myDialog.findViewById(R.id.passwordRepEditText)).setError(getString(R.string.passwordNotMatching));
            }





                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);

                try {

                    //Start AsyncTask
                    new loadingBar().execute().get();

                    Boolean resultOK = ackJSON.has("result");

                    if(resultOK){
                        //close dialog
                        myDialog.dismiss();

                        // Inflate the Layout
                        LayoutInflater inflater = getLayoutInflater();
                        View layout = inflater.inflate(R.layout.custom_toast_success,(ViewGroup) findViewById(R.id.custom_toast_layout_id));

                        Toast toastOK = new Toast(getApplicationContext());
                        toastOK.setDuration(Toast.LENGTH_LONG);
                        toastOK.setView(layout);
                        toastOK.show();

                    }else{
                        //Feedback both using Toasts and textedit
                        ((EditText) myDialog.findViewById(R.id.emailEditText)).setError(getString(R.string.userAlreadyIn));

                        // Inflate the Layout
                        LayoutInflater inflater = getLayoutInflater();
                        View layout = inflater.inflate(R.layout.custom_toast_erroruser,(ViewGroup) findViewById(R.id.custom_toast_no_user));

                        Toast toastNoUser = new Toast(getApplicationContext());
                        toastNoUser.setDuration(Toast.LENGTH_SHORT);
                        toastNoUser.setGravity(Gravity.TOP,0,50);
                        toastNoUser.setView(layout);
                        toastNoUser.show();

                    }

                } catch (IOException e) {
                    // Inflate the Layout
                    LayoutInflater inflater = getLayoutInflater();
                    View layout = inflater.inflate(R.layout.custom_toast_errorconnection,(ViewGroup) findViewById(R.id.custom_toast_no_noConn));

                    Toast toastNoConn = new Toast(getApplicationContext());
                    toastNoConn.setDuration(Toast.LENGTH_SHORT);
                    toastNoConn.setGravity(Gravity.TOP,0,50);
                    toastNoConn.setView(layout);
                    toastNoConn.show();
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }

        }
    });
}

class loadingBar extends AsyncTask<Void,Integer,JSONObject>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress.setProgress(0);
        progress.show();
    }

    @Override
    protected JSONObject doInBackground(Void... arg0)
    {
        ackJSON = null;
        try
        {
            for(int i=0;i<2;i++)
            {
                publishProgress(new Integer[]{i*10});
                Thread.sleep(1200);
            }
            String ack=HTTPRest.putNewUser(name.toString(),surname.toString(),email.toString(),hashPassword);
            ackJSON=new JSONObject(ack);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ackJSON;
    }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        super.onProgressUpdate(values);
        progress.setProgress(values[0].intValue());
    }

    @Override
    protected void onPostExecute(JSONObject result)
    {
        super.onPostExecute(result);
        progress.dismiss();
        ackJSON=result;
    }
}

如果代码中有任何错误,请告诉我

谢谢

5 个答案:

答案 0 :(得分:1)

一切正确,但您将更改此代码

  if(name.toString().isEmpty()){

        } 

因为当您没有输入任何值时,您的代码会出现问题,因此请不要检查您的情况。您的代码只会检查黑色空间。

答案 1 :(得分:1)

btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                String email = inputEmail.getText().toString();
                String password = inputPassword.getText().toString();

                // Check for empty data in the form
                if (email.trim().length() > 0 && password.trim().length() > 0) {
                    // login user
                    //checkLogin(email, password);
                    new AttemptLogin().execute();
                } else {
                    // Prompt user to enter credentials
                    Toast.makeText(getApplicationContext(),
                            "Please enter the credentials!", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });

class AttemptLogin  extends AsyncTask<String, String, String>{


        /** * Before starting background thread Show Progress Dialog * */

        boolean failure = false;

        @Override protected void onPreExecute() { 
            super.onPreExecute(); 
            pDialog = new ProgressDialog(LoginActivity.this); 
            pDialog.setMessage("Attempting for login..."); 
            pDialog.setIndeterminate(false); 
            pDialog.setCancelable(true); 
            pDialog.show(); 

        }

        @SuppressWarnings("deprecation")
        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // here Check for success tag

            int success; 
            String username = inputEmail.getText().toString(); 
            String password = inputPassword.getText().toString(); 

            try {

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");

                JSONObject json = jsonParser.makeHttpRequest(AppConfig.URL_LOGIN, "POST", params);

                // checking  log for json response
                //devraj......................
                Log.d("Login attempt", json.toString());

                 // success tag for json
                success = json.getInt(TAG_SUCCESS);



                 if (success == 1){

                     session.setLogin(true);

                     Log.d("Successfully Login!", json.toString());

                     Intent intent = new Intent(LoginActivity.this,Secondpage.class);

                     startActivity(intent);



                     return json.getString(TAG_MESSAGE);

            }
            else{
                return json.getString(TAG_MESSAGE);
              }
        }
            catch (JSONException e){

                e.printStackTrace();
            }

            return null;
        }

        /** * Once the background process is done we need to Dismiss the progress dialog asap * **/

        protected void onPostExecute(String message)
        {

            pDialog.dismiss();

            if (message != null){

                 Toast.makeText(First.this, message, Toast.LENGTH_LONG).show();
            }
        }
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

答案 2 :(得分:0)

您可以在onPostExecute()方法

中显示Toast

Asynctask的生命周期就像这样运行

onPreExecute() -> runs first

doInBackground() -> After onPreExecute 

`onPostExecute()` -> After doInBackground

因此,您可以在onPostExecute()

中更新用户界面或显示Toast

答案 3 :(得分:0)

您可以在AsyncTask的onPostExecute方法中进行工作

    @Override
        protected void onPostExecute(JSONObject result)
        {
            super.onPostExecute(result);
            progress.dismiss();
            ackJSON=result;

            //do your work here show toast or move to next activity
        }

答案 4 :(得分:0)

progress.setCancelable(false);