I am trying to send email using async method as shown below and everything is working fine..
Now I would like to display a dialog on successfull email.
This is my async code:
public void sending(View v) {
try {
LongOperation l=new LongOperation();
l.execute();
Toast.makeText(this, l.get(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
public class LongOperation extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try{GMailSender sender = new GMailSender("XXX@gmail.com","Pwd");
sender.sendMail("Sub",
"body",
"sender",
"recepient");
}
catch(Exception e){Log.e("error",e.getMessage(),e);return "Email Not Sent";}
return "Email Sent";
}
@Override
protected void onPostExecute(String result)
{
}
@Override
protected void onPreExecute()
{
}
@Override
protected void onProgressUpdate(Void... values)
{
}
In the above code if the mail is not sent I'm getting a toast as "Email Sent" and If not send the email I would get "Email Not Sent"
1)In the place of toasts I would like to display a dialog.
2)I have done it in onPostExecute
and it worked fine.
But here comes the problem.Suppose if there is no internet connection and user clicks the button both the toast "Email Not sent" and the dialog is displaying after onPostExecute method.
I would only like to display the dialog.
So how do I modify the above code inorder to remove the toasts and only get the dialog for successfull and unsuccessfull email.
Here is my dialog code:
new AlertDialog.Builder(MainActivity.this)
.setTitle("Info")
.setMessage("Sample message.")
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Log.d("AlertDialog","Positive");
}}).show();