Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailto Android: 'Unsupported action' error

I am new to this but what is wrong with my snippet of coding? I am getting the error: 'This action is not currently supported' when I select the link. Here is my code:

public void addEmail() {

    TextView txt = (TextView) findViewById(R.id.emailtext);

    txt.setOnClickListener(new View.OnClickListener(){


        public void onClick(View v){
            Intent intent = new Intent();
            String uriText =
                    "mailto:[email protected]" + 
                    "?subject=" + URLEncoder.encode("some subject text here") + 
                    "&body=" + URLEncoder.encode("some text here");

                Uri uri = Uri.parse(uriText);

                Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
                sendIntent.setData(uri);
                startActivity(Intent.createChooser(sendIntent, "Send email")); 

    }});

}

Many thanks!

like image 629
Georgina Bennett Avatar asked Dec 17 '14 14:12

Georgina Bennett


2 Answers

The problem is probably that you're running on one of the official Android emulators and you haven't yet set up an email account on it. The emulators open the com.android.fallback.Fallback activity when this happens, but this doesn't seem to happen on real-world devices.

You can detect this before trying to start the intent using this code:

ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
boolean hasEmailApp = emailApp != null && !emailApp.equals(unsupportedAction);
like image 140
Sam Avatar answered Nov 20 '22 02:11

Sam


Try this, it worked for me :

public void addEmail() {

     TextView txt = (TextView) findViewById(R.id.emailtext);

     txt.setOnClickListener(new View.OnClickListener(){

     public void onClick(View v){

            String[] emails = {"[email protected]"};
            String subject = "your subject";
            String message = "your message";

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, emails);
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.putExtra(Intent.EXTRA_TEXT, message);

            // need this to prompts email client only
            email.setType("message/rfc822");

            startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }});

}
like image 1
Abhinav Puri Avatar answered Nov 20 '22 03:11

Abhinav Puri