【发布时间】:2019-04-09 21:29:27
【问题描述】:
我在后台发送电子邮件时遇到问题。我试图将文本集作为电子邮件放入 EditText 中,但我的邮箱总是有 null,因为在我在 EditText 中输入任何内容之前总是调用 AsyncTask,然后在对话框中按“确定”。
final int partsCount = imageKeeperList.size();
class PhotoSend extends AsyncTask <Void, Void, Void>{
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {
final String username = "sampleemail@gmail.com";
final String password = "somepassword";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sampleemail@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("targetemail@gmail.pl"));
message.setSubject("Subject of email");
message.setText("Some text.");
Transport.send(message);
Log.d(TAG, "onInput: background");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(getContext(), "Sent.", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onInput: postExecute");
}
}
new MaterialDialog.Builder(getContext())
.content("Set description")
.inputType(InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE)
.input("short description", "", new MaterialDialog.InputCallback() {
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
if (input.length() == 0) partPicturesDescription = "No description";
else partPicturesDescription = dialog.getInputEditText().getText().toString();
dialog.dismiss();
Log.d(TAG, "onInput: preExecute");
}
}).show();
PhotoSend ps = new PhotoSend();
ps.execute(partPicturesDescription);
}
我的 onPreExecute() 方法中有对话框,但它保持不变,doInBackground 优先。
【问题讨论】:
标签: android multithreading asynchronous android-asynctask