launch Google Mail - Android App

Android examples for App:Popular App

Description

launch Google Mail

Demo Code


//package com.java2s;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class Main {
    public static void launchGoogleMail(Context a, Intent targetIntent) {
        defineAppToLaunch(a, targetIntent, "google", "gm");
        a.startActivity(Intent.createChooser(targetIntent, ""));
    }//from   w  ww .  ja  va  2 s.c o m

    public static boolean defineAppToLaunch(Context c, Intent targetIntent,
            String... keywords) {
        PackageManager pm = c.getApplicationContext().getPackageManager();
        List<ResolveInfo> activityList = pm.queryIntentActivities(
                targetIntent, 0);
        for (final ResolveInfo app : activityList) {
            if (containsAll(keywords, app)) {
                final ActivityInfo activity = app.activityInfo;
                final ComponentName name = new ComponentName(
                        activity.applicationInfo.packageName, activity.name);
                targetIntent.setComponent(name);
                return true;
            }
        }
        return false;
    }

    private static boolean containsAll(String[] keywords,
            final ResolveInfo app) {
        for (String keyword : keywords) {
            if (!app.activityInfo.packageName.contains(keyword)) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials