0

I am trying to send an email from my Android Activity like this:

@Override
        public void onClick(View view) {                       
            processdialog = new ProgressDialog(LoginScreen.this);
            processdialog.setTitle("Test mail sturen...");
            processdialog.setMessage("even wachten a.u.b..."); 
            processdialog.setCancelable(false);
            processdialog.show();
            testMailButton.setEnabled(false);
            AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();


                }

                @Override
                protected Void doInBackground(Void... arg0) {
                    try {
                        properties.setProperty("emailTo", emailContactField.getText().toString());
                        properties.setProperty("emailFrom", emailField.getText().toString());
                        properties.setProperty("passWFrom", passwordField.getText().toString());
                        String[] temp = { properties.getProperty("emailTo").toString()};
                        setupMail.updateUserInfo(temp,properties.getProperty("emailFrom"), properties.getProperty("passWFrom"));
                        loggedIn = setupMail.sendTestMail();
                        loginTryDone = true;
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    if (processdialog != null) {
                        processdialog.dismiss();
                        testMailButton.setEnabled(true);
                    }
                }

            };
            if(!loggedIn){
                task.execute();

But my UI doesn't give me the progressdialog and is just stuck for a couple of seconds untill the mail is send. Can anyone tell me what I did wrong here? Thanks

Black Magic
  • 2,706
  • 5
  • 35
  • 58

2 Answers2

0

Please use AsyncTask for this operation. The thread lock the ProgressDialog and for that you didn't see it.

@Override
public void onClick(View v) {
    if(!loggedIn){
        new MailTask().execute((Void) null);
    }
}

private class MailTask extends AsyncTask<Void, Void, Void> {

    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(Z.this);
        pd.setTitle("TITLE");
        pd.setMessage("MESSAGE");
        pd.show();

        testMailButton.setEnabled(false);

    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            properties.setProperty("emailTo", emailContactField.getText().toString());
            properties.setProperty("emailFrom", emailField.getText().toString());
            properties.setProperty("passWFrom", passwordField.getText().toString());
            String[] temp = { properties.getProperty("emailTo").toString()};
            setupMail.updateUserInfo(temp,properties.getProperty("emailFrom"), properties.getProperty("passWFrom"));
            loggedIn = setupMail.sendTestMail();
            loginTryDone = true;
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pd != null) {
            pd.dismiss();
            testMailButton.setEnabled(true);
        }
    }
}
Dragan
  • 328
  • 2
  • 12
  • I tried using ASyncTask before, but that didn't work for me either: http://stackoverflow.com/questions/19562885/android-asynctask-gui-stuck?rq=1 – Black Magic Oct 28 '13 at 14:10
  • Do not pass "context" to progress Dialog. In code above the method is in fragment, so I pass getActivity(); If you are in activity please use this pd = new ProgressDialog(YourActivity.this). After that dialog will show. – Dragan Oct 28 '13 at 14:17
  • But you put progress dialog out of AsyncTask. Put ProgressDialog "onPreExecute". And please separate button click and AsyncTask. – Dragan Oct 28 '13 at 14:43
  • I did exactly like you said, but no luck. It is still stuck for 2 or 3 seconds, and then it just shows the Dialog for like 0.2 seconds. – Black Magic Oct 28 '13 at 15:36
0

I have figured out the problem. It had to do with the fact that I made the Activity wait for the Asynctask. Wich made it unavailable. Thanks for everyone's help.

Black Magic
  • 2,706
  • 5
  • 35
  • 58