Reputation:
I've set up an email intent and tried launching it. Code below:
public void emailSummary(String emailText, String name) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, "");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "JustJava order for " + name);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
Log.v("MainActivity", "Finished setting up intent");
if (emailIntent.resolveActivity(getPackageManager()) != null) {
startActivity(emailIntent);
Log.v("MainActivity", "Sharing the order summary to email");
} else {
Log.d("MainActivity", "No email app installed!");
Toast.makeText(MainActivity.this, "Uh...No email app?", Toast.LENGTH_SHORT).show();
}
However, when I do press the Order button, the "Uh... No email app?" toast shows up. I have both Inbox and Gmail installed, but the Intent
does not seem to find any of them. Why?
Upvotes: 1
Views: 1753
Reputation: 20616
You can use this code :
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto",name, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "SOME_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
try {
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Log.v("MainActivity", "Sharing the order summary to email");
}
catch (android.content.ActivityNotFoundException ex) {
Log.d("MainActivity", "No email app installed!");
Toast.makeText(MainActivity.this, "Uh...No email app?", Toast.LENGTH_SHORT).show();
}
can you please explain what was wrong with my code?
I've tested your code and if (emailIntent.resolveActivity(getPackageManager()) != null) {
is bad to check if there is any email APP on your device. I've found this method and works fine : @Nitin answer
public static boolean isMailClientPresent(Context context){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
final PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);
if(list.isEmpty())
return false;
else
return true;
}
Upvotes: 2