问题
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?
回答1:
try to put break;
after each case
xxx {}
回答2:
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
回答3:
check the id of the button in design (xml file).
回答4:
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.
来源:https://stackoverflow.com/questions/10633965/android-email-intent