问题
How do I detect if an email client is configured on an Android device? If no email account is configured Android treats it as text message (I use android.content.Intent.ACTION_SEND). I want to prompt the user that no email client is configured.
回答1:
Instead of prompting the user that no email client is configured, consider wrapping the ACTION_SEND intent in a chooser via createChooser(). createChooser()
will return the intent that the user picked. If the user did not pick a valid email client, you can perhaps pop up an error message or provide the user with the settings screen to declare a valid email client.
Note that it is not possible to determine whether a sending application is a valid "email" application, just whether it is an application for sending. This is why the chooser should be used, so that the user will be the one to realize that they do not have an email client set up. Also note that it is extremely rare for a user to have no email client, as they must at least register with their google account when they start their phone (giving them access to gmail).
Here's an example for sending an email with a chooser:
sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/octet-stream");
sendIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {"myuser@gmail.com"});
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Body text of email message");
startActivity(Intent.createChooser(sendIntent, "Send Mail"));
来源:https://stackoverflow.com/questions/5888690/how-do-i-detect-if-an-email-client-is-configured-on-an-android-device