1

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?

idish
  • 3,190
  • 12
  • 53
  • 85
  • As far as i know, this isn't possible. You can't receive anything of the Dialog with Facebook, twitter ... One idea is to start a CountDownTimer when "share" is clicked, with for example 10 seconds and then check which Activity is on top of yours with ActivityManager. – Carsten Drösser Nov 30 '12 at 18:24
  • you can do one this using PackageManager you can check Current top Activity in Activity Stack after startActivity(Intent.createChooser(intent, "Dialog title text")); – ρяσѕρєя K Nov 30 '12 at 18:24
  • @imrankhan How can I do that? – idish Nov 30 '12 at 18:32
  • or if you really want to do something new then see this post http://stackoverflow.com/questions/4417019/how-to-get-the-user-selection-from-startactivityforresultintent-createchooserf CommonsWare answer. by creating a custom Chooser instead of using default device – ρяσѕρєя K Nov 30 '12 at 18:37
  • @imrankhan I've just found the following link: http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/ – idish Nov 30 '12 at 18:39
  • @imrankhan But I don't find the xml layout.. I want it to look like his list, any idea how can I achieve that? – idish Nov 30 '12 at 18:40
  • @idish : i see it he is missing layout for only AlertDialog row but you can create it yourself using LinerLayout with one textView and one ImageView – ρяσѕρєя K Nov 30 '12 at 18:54
  • @imrankhan alright, but also the basiclistview that he is passing in the setAdapter in the share function. Where do I create this one? – idish Nov 30 '12 at 18:56
  • @idish : basiclistview is row layout for just like in LIstView but he is passing row layout is to ShareIntentListAdapter constructor – ρяσѕρєя K Nov 30 '12 at 18:58
  • just take an example of ListView with custom row from google and integrate with it if you have any issue then comment me i will try it for you – ρяσѕρєя K Nov 30 '12 at 19:00
  • @imrankhan There's something very weird in his code, as you can see, he's passing 4 arguments in the creation of the adapter, but in the adapter constructor there are only 3 paramters.. right? what happens there? – idish Nov 30 '12 at 19:06
  • use this one `final ShareIntentListAdapter adapter = new ShareIntentListAdapter((Activity)context, R.layout.basiclistview, activities.toArray());` – ρяσѕρєя K Nov 30 '12 at 19:10
  • @imrankhan Alright, give me a few minutes, I think I'm getting it working. – idish Nov 30 '12 at 19:13
  • 1
    @imrankhan yep!! working great, thank you very much!! If you like you can post an answer so I could upvote and tick you answer. – idish Nov 30 '12 at 20:17
  • 1
    @idish : i think you can post better answer form me. so please post it as answer for others help who is looking for same issue.Thanks so much – ρяσѕρєя K Nov 30 '12 at 20:21

2 Answers2

3

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>
idish
  • 3,190
  • 12
  • 53
  • 85
  • +1vote you can also accept it as answer yourself and xml layout or `final ShareIntentListAdapter adapter = new ShareIntentListAdapter((Activity)context, R.layout.basiclistview, activities.toArray());` as change in current code – ρяσѕρєя K Nov 30 '12 at 20:43
  • @imrankhan ok edited, i just tried to accept my answer, but I can only in just 2 days. anyways, thank you once again. – idish Nov 30 '12 at 20:58
1

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

Community
  • 1
  • 1
zonabi
  • 746
  • 6
  • 20