0

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();
coder
  • 13,002
  • 31
  • 112
  • 214
  • I'm confused. If you don't want the `Toast` to show then why don't you take that out? What problem does that cause? – codeMagic Nov 28 '13 at 18:49
  • This code I have took from the other app but I am new to this android concept..So can you show me how to do that ? – coder Nov 28 '13 at 18:50
  • Just delete the `Toast`. You said the `Dialog` code is in your `onPostExecute()` so I don't see the problem – codeMagic Nov 28 '13 at 18:50
  • Ok..I will delete that toast..But where exactly do I need to use my dialog code in order to wok it in a proper way ? The problem is its working in OnPostExecute method..But If there is no internet connection on the user end then also it shows the same dialog after PostExecute which shouldn't happen – coder Nov 28 '13 at 18:52
  • I have posted an answer. Hopefully that makes sense. – codeMagic Nov 28 '13 at 18:56

1 Answers1

1

Still not completely sure I understand the problem but maybe this is what you want.

public void sending(View v) {
try {   
    LongOperation l=new LongOperation();
    l.execute();
} 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) 
{
    if ("Email Sent".equals(result))
    {
        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();

}
@Override
protected void onPreExecute() 
{
}

@Override
protected void onProgressUpdate(Void... values) 
{
}

I simply removed the Toast since you don't want that at all. Then I placed the Dialog code in onPostExecute() and checked the result passed to it from doInBackground(). I only showed it if the result is "Email Sent". You can change that to pass back a different result from doInBackground() if it isn't sent and show a different message in your Dialog.

Edit

I almost forgot to mention, you almost never want to use .get() on an AsyncTask. It is a blocking call which means everything will halt until your task is finished. You update the UI in any task method besides doInBackground() or you use an interface with a callback.

See this answer if you need an interface

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93