我正在尝试从我的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();
但是我的用户界面并没有给我进行进度调整,只是停留了几秒钟,直到发送邮件为止。谁能告诉我这里做错了什么? 感谢
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:0)
我已经找到了问题所在。这与我使Activity等待Asynctask的事实有关。这让它无法使用。感谢大家的帮助。