share App via Intent - Android Intent

Android examples for Intent:Open App

Description

share App via Intent

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

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;

public class Main {
    public static void shareApp(Context context, String chooserTitle,
            String message, String messageSubject) {
        message = message + getShareUrl(context);

        Intent mainIntent = new Intent();
        mainIntent.setAction(Intent.ACTION_SEND);
        mainIntent.setType("text/plain");
        mainIntent.putExtra(Intent.EXTRA_TEXT, message);
        mainIntent.putExtra(Intent.EXTRA_SUBJECT, messageSubject);
        mainIntent.setPackage("com.android.bluetooth");

        PackageManager pm = context.getPackageManager();

        Intent sentIntent = new Intent(Intent.ACTION_SEND);
        sentIntent.setType("text/plain");
        sentIntent.putExtra(Intent.EXTRA_SUBJECT, messageSubject);
        sentIntent.putExtra(Intent.EXTRA_TEXT, message);

        Intent openInchooser = Intent.createChooser(mainIntent,
                chooserTitle);/*from w w w.j  a v a2  s . co  m*/

        List<ResolveInfo> resinfo = pm.queryIntentActivities(sentIntent, 0);
        List<LabeledIntent> intentList2 = new ArrayList<LabeledIntent>();

        for (int i = 0; i < resinfo.size(); i++) {
            ResolveInfo ri = resinfo.get(i);
            String packageName = ri.activityInfo.packageName;
            if (packageName.contains("bluetooth")) {
                continue;
            }

            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName,
                    ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, messageSubject);
            intent.putExtra(Intent.EXTRA_TEXT, message);

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

        String[] shortOrder = { "mms", "whatsapp", "tencent.mm", "hike",
                "line", "nimbuzz", "chaton", "android.email", "android.gm" };
        for (int i = shortOrder.length - 1; i >= 0; i--) {
            String pkgpart = shortOrder[i];
            for (int j = 0; j < intentList2.size(); j++) {
                LabeledIntent labeledIntent = intentList2.get(j);
                if (labeledIntent.getSourcePackage().contains(pkgpart)) {
                    LabeledIntent remove = intentList2.remove(j);
                    intentList2.add(0, remove);
                    break;
                }
            }
        }

        LabeledIntent[] extIntents = new LabeledIntent[intentList2.size()];
        for (int i = 0; i < intentList2.size(); i++) {
            extIntents[i] = intentList2.get(i);
        }

        openInchooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extIntents);
        context.startActivity(openInchooser);

    }

    private static String getShareUrl(Context context) {

        return "\n" + "http://play.google.com/store/apps/details?id="
                + context.getPackageName();
    }
}

Related Tutorials