Question

I've created an application which shares to facebook, twitter etc. But I want to perform different functions dependent on who the user is sharing to, for instance if the user is sharing to Facebook do one thing but if the user shares to twitter do another.

How do I do this?

My code so far is below:

private void ShareSub() {
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(
            Intent.EXTRA_TEXT,
            "You've just shared "
                    + "Awesome");
    startActivity(i);
}

Further explanation of above code:

When the user presses the "menu_item_share" button in the action bar, the function above is triggered. This function then makes a box like the image below appear, which allows the user to select a particular option, e.g. Facebook or twitter. The Intent.EXTRA_TEXT is then shared to that particular app.

What I'm wanting is, when the user clicks for instance Facebook, a particular function is called e.g. Sharing via their (Facebook's) api etc.

enter image description here

I'm not using ShareActionProvider like below:

   <item
        android:id="@+id/your_share_item"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="ifRoom"
        android:title="@string/share"/>

Instead, I've created a simple button which calls the function ShareSub:

<item
    android:id="@+id/menu_item_share"
    android:icon="@android:drawable/ic_menu_share"
    android:showAsAction="ifRoom"
    android:title="Share"/>

The reason I've chosen to do it this way is, I don't want the recently shared to button to appear, as in the image below. This is causing me problems when I attempt to use the code suggested below because I haven't used ShareActionProvider.

enter image description here

Was it helpful?

Solution

You can't directly find out which app was chosen, but you can display your own Chooser dialog.

Instead of calling startActivity(i), get a list of all activities (facebook, twitter etc) that are registered to handle your intent using queryIntentActivities().

List<ResolveInfo> activities = getPackageManager().queryIntentActivities (i, 0);

Each ResolveInfo is an app that can handle your intent. Now you can show an AlertDialog containing a list of labels or icons. When the user selects a label from the list, you can handle what do to on an app-specific basis in the click handler for the dialog.

EDIT: Here's a full working example

private void ShareSub() {
    final Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_TEXT,"text");

    final List<ResolveInfo> activities = getPackageManager().queryIntentActivities (i, 0);

    List<String> appNames = new ArrayList<String>();
    for (ResolveInfo info : activities) {
        appNames.add(info.loadLabel(getPackageManager()).toString());
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Complete Action using...");
    builder.setItems(appNames.toArray(new CharSequence[appNames.size()]), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            ResolveInfo info = activities.get(item);
            if (info.activityInfo.packageName.equals("com.facebook.katana")) {
                // Facebook was chosen
            } else if (info.activityInfo.packageName.equals("com.twitter.android")) {
                // Twitter was chosen
            }

            // start the selected activity
            i.setPackage(info.activityInfo.packageName);
            startActivity(i);
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

OTHER TIPS

Use presses a button in the action bar which triggers the above function.

If you used a ShareActionProvider instead, you could detect when a user selects a particular app from the list by implementing a ShareActionProvider.OnShareTargetSelectedListener. Otherwise you won't be able to detect which app is selected unless you implement your own activity chooser.

Here's an example:

menu

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/your_share_item"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="ifRoom"
        android:title="@string/share"/>

</menu>

Implementation

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate your menu
    getMenuInflater().inflate(R.menu.share, menu);

    // The Intent you want to share
    final Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_TEXT, "Test");

    // Set up your ShareActionProvider
    final ShareActionProvider shareActionProvider = (ShareActionProvider) menu.findItem(
            R.id.your_share_item).getActionProvider();
    shareActionProvider.setShareIntent(i);
    shareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {

        @Override
        public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
            final String packageName = intent.getComponent().getPackageName();
            if (packageName.equals("com.facebook.katana")) {
                // Do something for Facebook
            } else if (packageName.equals("com.twitter.android")) {
                // Do something for Twitter
            }
            return false;
        }

    });
    return super.onCreateOptionsMenu(menu);
}

Although this question has an accepted answer, things have changed, and now there is a better approach, which is the recommended way of achieving this.

Using "Android Sharesheet"

Here's the documentation: https://developer.android.com/training/sharing/send#share-interaction-data

While creating the IntentChooser, you can pass a pending intent, which will trigger a broadcast receiver within your app.

The data provided by the onReceive method of that Broadcast Receiver will give you the name of the component that was used to handle the SEND intent.

Code for adding a pending intent to the SEND intent:

Intent share = new Intent(ACTION_SEND);
...
PendingIntent pi = PendingIntent.getBroadcast(myContext, requestCode,
        new Intent(myContext, MyBroadcastReceiver.class),
        PendingIntent.FLAG_UPDATE_CURRENT);
share = Intent.createChooser(share, null, pi.getIntentSender());

Code for retrieving the details of the component that was chosen by the user:

@Override public void onReceive(Context context, Intent intent) {
  ...
  ComponentName clickedComponent = intent.getParcelableExtra(EXTRA_CHOSEN_COMPONENT);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top