3

Currently I have a pretty standard ACTION_SEND intent to share information from inside my app. Code is similar to what is below:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, text);
    context.startActivity(Intent.createChooser(intent, title));

Now, if the user has the Facebook app installed on their phone then Facebook shows up as an option for the intent chooser. However, I want to intercept the user's click on "Facebook" and use the Facebook SDK to carry out the task instead of the Facebook application already on the users phone. Is there a way to intercept the onClick for the Facebook option? Thanks!

mhenry
  • 1,487
  • 5
  • 17
  • 31

2 Answers2

7

Here is a link to my blog post with a detailed solution, including code and screen shots.

http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/

As quoted from that article:

[W]e first create a new action_send intent and set the type to text/plain. Next, we create a List. We make the call to the package manager to query for all Activities with the action_send intent registered. This call returs a list of ResolveInfo objects, each corresponding to an Activity on the device that claims to handle send actions.

[Then, r]ather than launching the action_send intent and letting it create its own dialog (one that we would have no control over), we will build our own with the AlertDialog.Builder. First we give it a title, then set its adapter to the list adapter we just created with the activities list as our data set[.]

The next important piece we need to look at is the OnClick listener we gave the Builder. To find which item the user clicked, we use the adapter.getItem(int which) method. This will return the object in that position of our original list, in this case, a ResolveInfo object corresponding to the selected Activity. For my case in particular, I only care about separating things into two groups: Facebook and not Facebook. To do this, I simply check if the selected Activity’s package name contains ‘facebook’. If it does not, I create a new action_send intent, set the class name to the selected activity, and launch it. However, if the package name DOES contain ‘facebook’, I instantiated my personal PostToFacebookDialog object which will create a basic Android dialog with post and cancel buttons, and will post to Facebook directly using the Graph API, thus circumventing the user’s installed Facebook app.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
italianmike
  • 106
  • 7
  • @alexander-lucas, it is not true that you cannot change the functionality of the list. I encourage you to read my post on how to do exactly that. badhairday, my post implements the exact functionality you are requesting and includes code. Please take a look at get back to me if you have questions. – italianmike Jan 13 '12 at 21:33
0

If you have some functionality built into your own application to post to Facebook, then there's no reason to be creating a chooser and having the user choose an external app in the first place. That said, if you're using the SDK and have some functionality built into your own app to do this, then don't leave the application. Just create an intent to fire into a specific class, like so:

Intent intent = new Intent(this.getActivity(), YourPostingActivity.class);
...//  Add stuff
startActivity(intent);

It's not really possible to modify the link to Facebook within the chooser (not to mention, a poor user experience to intercept their attempt to handle an action with the app of their choice). That said, if you register the relevant Activity in your own app as a receiver of the ACTION_SEND intent, it'll show up in the list, and users will have the option of selecting your app instead of the native Facebook app.

Alexander Lucas
  • 22,171
  • 3
  • 46
  • 43
  • But what if I want to retain the ability to send things via MMS, email and whatever else the user has installed on their phone. – mhenry Dec 02 '11 at 19:27
  • The problem with registering my own application as a receiver of the ACTION_SEND intent is that the official Facebook application will still apear in the list. I do not want this to happen. – mhenry Dec 03 '11 at 00:24