我使用的AsyncTask發送電子郵件:給予反饋給用戶時的AsyncTask failes
class AsyncSendEmail extends AsyncTask<String, Void, String> {
private Exception exception;
Session session;
public AsyncSendEmail(Session _session)
{
session = _session;
}
protected String doInBackground(String... parms) {
try {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(parms[1].getBytes(), "text/plain"));
message.setSender(new InternetAddress(parms[2]));
message.setSubject(parms[0]);
message.setDataHandler(handler);
if (parms[2].indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(parms[2]));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(parms[2]));
Transport.send(message);
return "SUC";
} catch (Exception e) {
e.printStackTrace();
return "FLD";
}
}
的methos調用:
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
new AsyncSendEmail(session).execute(subject,body,sender,recipients);
}
當我的 「doInBackground」 方法excecutes和failes我想提醒用戶使用通知。
我怎樣才能做到這一點?我如何從方法調用中捕獲異常?
或者我怎樣才能得到一個上下文,所以我可以顯示通知?
您可以製作一個標誌,指示發送失敗並在onPostExecute中檢查標誌併發出通知 –