Search code examples
androidshareactionprovider

Detecting sharer app when using share action provider


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; 


    }

Solution

  • 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.