idish
idish

Reputation: 3270

Get the selected share intent

I am using the following code to share some text via gmail,facebook,twitter, etc:

  Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "The status update text");
startActivity(Intent.createChooser(intent, "Dialog title text"));

The thing is that I want to check if the selected intent is Facebook, and if it is, I will do some other coding than prompt the share intent as usual.

Is it possible?

Upvotes: 1

Views: 3504

Answers (2)

zonabi
zonabi

Reputation: 746

had this same exact issue, wanting to detect facebook sharing option and take a different logic path (mostly because facebook sharing via regular android sharing is limited)

my solution

public void shareIt(View view){
    //sharing implementation
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com";

    PackageManager pm = view.getContext().getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
    for(final ResolveInfo app : activityList) {

         String packageName = app.activityInfo.packageName;
         Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
         targetedShareIntent.setType("text/plain");
         targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "share");
         if(TextUtils.equals(packageName, "com.facebook.katana")){
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://someurl.com");
         } else {
             targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
         }

         targetedShareIntent.setPackage(packageName);
         targetedShareIntents.add(targetedShareIntent);

    }

    Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share Idea");

    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
    startActivity(chooserIntent);

}

which is listed on this Q and A Branching the Android Share Intent extras depending on which method they choose to share

Upvotes: 1

idish
idish

Reputation: 3270

I've just found the following link : http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/

It describes how to do this kind of task, all you need to do is create a row layout, and you're done. I tried it and it works great!

EDIT: There's a mistake in the article in the show function, it should be the following:

final ShareIntentListAdapter adapter = new ShareIntentListAdapter((Activity)context, R.layout.basiclistview, activities.toArray());

Thanks to @imram khan for mentioning that.

If anybody wants to use my xml row layout:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


   <TextView android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_alignParentRight="true"
    android:layout_marginRight="50dp"
/>


    <ImageView android:id="@+id/logo2"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:padding="10dp"
    android:src="@drawable/number1"
    android:layout_gravity="center_vertical"
    android:layout_alignParentRight="true"/>

</RelativeLayout>

Upvotes: 3

Related Questions