How to customize share intent in Android? How to customize share intent in Android? android android

How to customize share intent in Android?


There is a way to directly open the intent you wants. You can get the list of intents and open only one.

See this code:

private void initShareIntent(String type) {    boolean found = false;    Intent share = new Intent(android.content.Intent.ACTION_SEND);    share.setType("image/jpeg");    // gets the list of intents that can be loaded.    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);    if (!resInfo.isEmpty()){        for (ResolveInfo info : resInfo) {            if (info.activityInfo.packageName.toLowerCase().contains(type) ||                     info.activityInfo.name.toLowerCase().contains(type) ) {                share.putExtra(Intent.EXTRA_SUBJECT,  "subject");                share.putExtra(Intent.EXTRA_TEXT,     "your text");                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myPath)) ); // Optional, just if you wanna share an image.                share.setPackage(info.activityInfo.packageName);                found = true;                break;            }        }        if (!found)            return;        startActivity(Intent.createChooser(share, "Select"));    }}

If you wanna open twitter, do that:

initShareIntent("twi");

if facebook:

initShareIntent("face");

if mail:

initShareIntent("mail"); // or "gmail"

If you wanna show a list of intents that match with the type, insted of use the first mach, see this post: Android Intent for Twitter application


No you can't. Intent's are supposed to work this way. If you have to force a particular app to open, use explicit intents if the target apps support those. Without knowing the package names or the component names of the target apps, or the type or mime type of data, you can't force a particular app to work on generalized intents.


First, list all apps to share.

private java.util.List<ResolveInfo> showAllShareApp() {    java.util.List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();    Intent intent = new Intent(Intent.ACTION_SEND, null);    intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");    intent.setType("text/plain");    PackageManager pManager = getPackageManager();    mApps = pManager.queryIntentActivities(intent,            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);    return mApps;}

Then, select one

    private void share(ResolveInfo appInfo) {        Intent sendIntent = new Intent();        sendIntent.setAction(Intent.ACTION_SEND);        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");        if (appInfo != null) {            sendIntent                    .setComponent(new ComponentName(                            appInfo.activityInfo.packageName,                            appInfo.activityInfo.name));        }        sendIntent.setType("text/plain");//        startActivity(Intent.createChooser(sendIntent, "Share"));        startActivity(sendIntent);    }

Here's an sample project.Hope it helps.