Launch an email intent if the device is capable - Android android.content

Android examples for android.content:Intent

Description

Launch an email intent if the device is capable

Demo Code

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class Main {

  private static final String LOG_TAG = "AdapterUtils";

  /**/*from   w w  w.  j a  va 2s  .  c om*/
   * Launch an email intent if the device is capable.
   *
   * @param activity
   *          The parent activity (for context)
   * @param addr
   *          The address to email (not the full URI)
   * @param text
   *          The email body
   */
  public static void launchEmailIntent(final Activity activity, String addr, String text) {
    // create email intent
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { addr });
    emailIntent.setType("text/plain");
    // make sure there is an activity which can handle the intent.
    PackageManager emailpackageManager = activity.getPackageManager();
    List<ResolveInfo> emailresolveInfos = emailpackageManager.queryIntentActivities(emailIntent, 0);
    if (emailresolveInfos.size() > 0) {
      activity.startActivity(emailIntent);
    }
  }

}

Related Tutorials