0

Is there any way I can detect which sharer app is selected when using share action provider,so that I can send different messages for different apps? I am using following method for share action provider,

mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();

        mShareActionProvider.setShareIntent(getDefaultShareIntent());

and intent,

 public Intent getDefaultShareIntent(){
              String message = Fname + Mobileno + Homeno + Workmail + Homemail
                + Gtalk + Skype + Address + Company + Title + Website;
      Intent shareIntent = new Intent(Intent.ACTION_SEND);
              shareIntent.putExtra(Intent.EXTRA_TEXT, message); 


         return shareIntent; 


    }
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90

3 Answers3

6

UPDATE:

The simplest solution is:

@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
  String shareTarget = intent.getComponent().getPackageName();
  ...
}

No need to copy files or anything.


Copy some files from either the Android source, or ActionBarSherlock, if you happen to use the latter:

  • ActivityChooserModel.java
  • ActivityChooserView.java
  • ShareActionProvider.java

Make sure you reference these files, not the original ones from your app.

In ActivityChooserModel.java, modify this:

if (mActivityChoserModelPolicy != null) {
  ResolveInfo info = getActivity(index);
  choiceIntent.putExtra("user_selected_activity", (info.activityInfo != null) ? info.activityInfo.packageName : info.serviceInfo.packageName);
  final boolean handled = mActivityChoserModelPolicy.onChooseActivity(this, choiceIntent);
  if (handled)
    return null;
}

and it will store the package name of the selected activity into the intent. You can then read it in your handler:

@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
  String shareTarget = intent.getStringExtra("user_selected_activity");
  ...
}

and decide what to handle differently depending on the activity selected.

Gábor
  • 9,466
  • 3
  • 65
  • 79
  • But Android developers says Modifying the intent is not permitted and any changes to the latter will be ignored. For reference http://developer.android.com/reference/android/widget/ShareActionProvider.OnShareTargetSelectedListener.html#onShareTargetSelected%28android.widget.ShareActionProvider,%20android.content.Intent%29 – sandeepmaaram Dec 04 '14 at 04:57
  • In these cases, you generally don't want to modify the intent itself, just to set the extras differently (eg. send HTML to apps that handle it, text to those who don't). – Gábor Dec 29 '14 at 16:20
3

Try this.

myShareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
            @Override
            public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
                String shareTarget = intent.getComponent().getPackageName();
                return false;
            }
        });
Zafer
  • 316
  • 4
  • 13
  • Here we works but, we can not modify intent here http://developer.android.com/reference/android/widget/ShareActionProvider.OnShareTargetSelectedListener.html#onShareTargetSelected%28android.widget.ShareActionProvider,%20android.content.Intent%29 – sandeepmaaram Dec 04 '14 at 05:13
2

Doesn't look like it. But there's nothing stopping you from copying and pasting it into your project and modifying it as you see fit.

j__m
  • 9,392
  • 1
  • 32
  • 56
  • If you have installed "Sources for Android SDK" using the Android SDK Manager, you can right-click ShareActionProvider and select "Open Declaration" to see the source code. ShareActionProvider doesn't do anything you can't do yourself, so if you want you could copy & paste the code into your own class and modify it. – j__m Mar 21 '13 at 05:10