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

Android examples for Hardware:Device Feature

Description

Launch an email intent if the device is capable.

Demo Code

/*/*from w  ww  .  jav  a  2 s.c om*/
 * Copyright 2015 OpenMarket Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import android.app.Activity;

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

import android.util.Log;
import java.util.List;

public class Main {
    private static final String LOG_TAG = "AdapterUtils";

    /** 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) {
        Log.i(LOG_TAG,
                "Launch email intent from " + activity.getLocalClassName());
        // 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