0

我正在尝试从我的 Android Activity 发送一封电子邮件,如下所示:

@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();

但是我的用户界面没有给我进度对话框,只是卡住了几秒钟,直到发送邮件。谁能告诉我我在这里做错了什么?谢谢

4

2 回答 2

0

请使用 AsyncTask 进行此操作。该线程锁定了 ProgressDialog,因此您没有看到它。

@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);
        }
    }
}
于 2013-10-28T13:55:38.083 回答
0

我已经弄清楚了问题所在。这与我让 Activity 等待 Asynctask 的事实有关。使其无法使用。感谢大家的帮助。

于 2013-10-29T07:14:34.020 回答