0

I've set up two buttons. One opens the compose sms intent and the other opens the compose email intent. The sms intent works fine but the email button doesnt respond. Ive created a categorychooser but that doesnt show up....UNTIL I click the sms button

This is my code

case R.id.button2:
    {
        String phoneNumber = "xxxxxxxxxx";``
        Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
        smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
        smsIntent.setType("vnd.android -dir/mms-sms");
        smsIntent.setData(Uri.parse("sms:"+phoneNumber));
        startActivity(smsIntent);


    }
    case R.id.button3:
    {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"xxxxxxxx@gmail.com"});
        emailIntent.setType("plain/text");
        startActivity(Intent.createChooser(emailIntent, "Send email..."));

    }

Any ideas?

QuantumFoam
  • 1
  • 1
  • 2

4 Answers4

5

try to put break; after each case xxx {}

Lucifer
  • 29,392
  • 25
  • 90
  • 143
belhassine
  • 151
  • 1
  • 4
  • didnt help. now the categorychooser doesnt appear even after selecting the sms option – QuantumFoam May 17 '12 at 11:02
  • If you don't have a break statement at the end of a case, then if you click R.id.button2, the code in all the cases below will also be executed. If something is broken when you add a break, then it would have been broken anyway. At least you're 1 step closer to the answer – Mike T May 18 '12 at 06:43
0

The mail intent is just available if you use it on a real device where a mail client is installed. I guess you have problems in the emulator. To make it work there, you need a client installed that supports this intent. I guess you get a error like:

android.content.ActivityNotFoundException: No Activity found to handle Intent 
Sven
  • 1
  • 3
0

check the id of the button in design (xml file).

Rakesh Burbure
  • 1,045
  • 12
  • 27
0

The developer guide for common intents warns about the case that if no app with an Activity responding to the intent an exception is being thrown:

Caution: If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.

For example:

private void sendEmail(String address) {
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"xxxxxxxx@gmail.com"});
    emailIntent.setType("plain/text");
    if (emailIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(Intent.createChooser(emailIntent, "Send email..."));
    } else {
        // TODO: Tell your user about it
    }
}

Alternatively you could do the check in your onCreate() to hide the button altogether.

robocab
  • 777
  • 5
  • 15