Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Email Sent or Not in onActivity Result

Tags:

android

email

I want to detect whether person has send the email or pressed back button or discarded it, in my onActivityResult Method. How can I do the same.

I am doing it like this

String[] reciepients = result.toArray(new String[result.size()]);
        Intent email = new Intent(android.content.Intent.ACTION_SEND);
        email.putExtra(android.content.Intent.EXTRA_EMAIL,reciepients);
        email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        email.putExtra(android.content.Intent.EXTRA_TEXT, emailMessage);
        email.setType("text/plain");
        email.setType("message/rfc822");
        Intent intent =  Intent.createChooser(email, "Choose Email Client");
        //context.start(Intent.createChooser(email, "Choose Email.."));
        ((Activity)context).startActivityForResult(intent, FinalVariables.SEND_EMAIL);

onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {


        case FinalVariables.SEND_EMAIL:
            if(resultCode==Activity.RESULT_CANCELED){
                showToast("Cancelled");
            }
            else if(resultCode==Activity.RESULT_OK){
                showToast("Success");
            }
            break;

        default:
            break;
        }

    }

But this way it always gives me cancelled, as Email client never sets the RESULT as RESULT_OK.

So how can I fulfill my requirement. Please suggest.

Thanks

like image 886
Gaurav Arora Avatar asked Jan 13 '23 03:01

Gaurav Arora


1 Answers

After reading tones of staff on this issue I understood that there is no way to know exactly if the user pressed "Send" or just canceled.

But there is a way to find out at least if the user has opened any mail client application or pressed back from the "Complete action using" dialog. (In my case I just wanted to finish activity if the user opened mail client and do nothing if the user pressed back from the dialog). The trick is very simple.

As the dialog is a floating window, when it is being shown over the activity, only the onPause() method is being called in activity, but when the user chose a mail client and it is being opened the onStop() method of activity is also being called. So you can start ACTION_SEND with startActivityForResult() :

startActivityForResult(intent, CODE_SEND);

and also have a boolean flag that you will change in onPause() and onStop():

public class MainActivity extends Activity {
...

private boolean mailClientOpened = false;

@Override
protected void onResume() {
   super.onResume();
   mailClientOpened = false;
}

@Override
protected void onStop() {
    super.onStop();
    mailClientOpened = true;
}

and in your onActivityResult() you can check the requestCode and the boolean mailClientOpened to know if the client was opened or user canceled the dialog:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CODE_SEND && mailClientOpened){
    finish(); // Or do something else that you need to do when you know that user at least opened the mail client app
        }
    }

P.S. I know that this is not an exact answer to the question but I hope this can be useful to someone.

like image 65
Andranik Avatar answered Jan 25 '23 04:01

Andranik