Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LabeledIntent;

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static final String PACKAGE_MMS = "mms";
    private static final String PACKAGE_GMAIL = "android.gm";
    private static final String PACKAGE_EMAIL = "android.email";
    private static final String INTENT_TYPE_MSG = "message/rfc822";
    private static final String INTENT_TYPE_TEXT = "text/plain";

    public static void openMailChooser(Context context, String text, String[] mails, String subject) {
        Intent mailIntent = new Intent();
        mailIntent.setAction(Intent.ACTION_SEND);
        mailIntent.putExtra(Intent.EXTRA_TEXT, text);
        mailIntent.putExtra(Intent.EXTRA_EMAIL, mails);
        mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        mailIntent.setType(INTENT_TYPE_MSG);

        PackageManager pm = context.getPackageManager();
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType(INTENT_TYPE_TEXT);

        Intent openInChooser = Intent.createChooser(mailIntent, "");

        List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        for (ResolveInfo ri : resInfo) {
            String packageName = ri.activityInfo.packageName;
            if (packageName.contains(PACKAGE_EMAIL)) {
                mailIntent.setPackage(packageName);
            } else if (packageName.contains(PACKAGE_MMS) || packageName.contains(PACKAGE_GMAIL)) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
                intent.setAction(Intent.ACTION_SEND);
                intent.setType(INTENT_TYPE_TEXT);
                if (packageName.contains(PACKAGE_MMS)) {
                    intent.putExtra("subject", subject);
                    intent.putExtra("sms_body", text);
                    intent.putExtra("address", mails[0]);
                    intent.setType(INTENT_TYPE_MSG);
                } else if (packageName.contains(PACKAGE_GMAIL)) {
                    intent.putExtra(Intent.EXTRA_TEXT, text);
                    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                    intent.putExtra(Intent.EXTRA_EMAIL, mails);
                    intent.setType(INTENT_TYPE_MSG);
                }

                intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
            }
        }

        LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);

        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        context.startActivity(openInChooser);
    }
}