I want to enable the user of my android app to post some data on fb,twitter and email it to someone as well. I am using Intent.ACTION_SEND for this. I can add the email subject and add test as Intent.EXTRA_TEXT. But I want different texts to be sent to dirrerent applications. Like the text to be sent to twitter will be short, the text to be sent to facebook will have a link and a shot description, and the on ein email have all the content. How can I achieve such a functionality? At most I can let facebook and twitter take the same text but different from what it is in email.
2 Answers
First, create an Intent representing what you want to potentially e-mail, post twitter, etc. Put some good default values in the Intent.EXTRA_TEXT and the subject. Then call, Intent.createChooser() with your intent. This method will return an Intent representing which Activity the user selected. Now, here's where we add the customization you want. Examine the Intent that is returned like so:
Intent intentYouWantToSend = new Intent(Intent.ACTION_SEND);
intentYouWantToSend.putExtra(Intent.EXTRA_TEXT, "Good default text");
List<ResolveInfo> viableIntents = getPackageManager().queryIntentActivities(
intentYouWantToSend, PackageManager.MATCH_DEFAULT_ONLY);
//Here you'll have to insert code to have the user select from the list of
//resolve info you just received.
//Once you've determined what intent the user wants, store it in selectedIntent
//This details of this is left as an exercise for the implementer. but should be fairly
//trivial
if(isTwitterIntent(selectedIntent)){
selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for twitter");
}
else if(isFacebookIntent(selectedIntent)){
selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for facebook");
}
startActivity(selectedIntent);
By examining the Intent that is returned by Intent.createChooser, we can determine how we need to modify it before launching it. You'll have to implement the isTwiterIntent and isFacebookIntent function yourself though. I imagine this will be relatively easy though, as you probably just have to examine the context of the Intent. I'll do a little more research and see if I can't find an exact solution for determining if an Intent is for Twitter or Facebook, or whatever and try to give you a more complete answer.

- 30,445
- 13
- 78
- 102
-
The problem will be that you wont know which apps go to twitter and which to facebook so it will always only work with a limited set.. – Manfred Moser Oct 19 '11 at 22:40
-
@ManfredMoser I assume you're referring to apps like tweetdeck. Such a thing is orthogonal to the question at hand and doesn't really get at the crux of what the questioner is trying to ask here. – Kurtis Nusbaum Oct 19 '11 at 22:46
-
1Hm.. not sure I understand. You will basically get a bunch of intents back that are various intents exposed by various apps. Now you would have to know what they do for all apps out there to really make this work from what I understand. Otherwise it is totally feasible that you send a facebook type message to an intent that then ends up sending it to twitter... – Manfred Moser Oct 19 '11 at 22:50
-
@ManfredMoser That's not really a realistic scenario though. – Kurtis Nusbaum Oct 19 '11 at 22:53
-
1Well.. thats what I observe in my implementation so ymmv – Manfred Moser Oct 20 '11 at 01:51
-
@ManfredMoser Really? You don't say. What's your implementation? Do you have an app I can look at. I'd be really curious to see where the flow you've described takes place. – Kurtis Nusbaum Oct 22 '11 at 20:38
-
Alright ... I looked at this again in my actual codebase. From what I can tell this approach does not actually work since the chooser is only created with startActivity and not before so at that stage your code will never run. Do you have this actually working somewhere I can look at it because it surely does not work for me.. also see e.g. http://stackoverflow.com/questions/4064848/how-to-exclude-your-own-app-from-the-share-menu – Manfred Moser Oct 24 '11 at 16:48
-
@ManfredMoser you're right, I've modified my answer to address your point. – Kurtis Nusbaum Oct 24 '11 at 17:33
-
Love the comment.. this is an exercise for the implementer ;-) – Manfred Moser Oct 24 '11 at 17:35
-
Can you tell I've had experience as a TA? ;) – Kurtis Nusbaum Oct 24 '11 at 17:37
-
oh btw the call is missing the flags argument.. that is your exercise to update.. – Manfred Moser Oct 24 '11 at 17:41
-
On my app on my tablet I get 17 items back. Good luck deciding which ones are for twitter, for facebook, for g+ and for all of them and so on for all apps across the markets.. – Manfred Moser Oct 24 '11 at 17:53
-
@ManfredMoser What would you do instead? I sincerely invite you to post an alternate solution. – Kurtis Nusbaum Oct 24 '11 at 17:57
-
I would not do anything and leave it up to the user to select where they want to send the data to and update it if desired. And therefore only do a generic message. But that is not really an answer to the question, is it? – Manfred Moser Oct 24 '11 at 18:31
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(sharingIntent, 0);
By this code you get list of applications that support Intent.ACTION_SEND action. After that u can built a Alert Dialog to display those applications.
then on click listener of the particular application you can make your changes as given code
public void onClick(DialogInterface dialog, int which)
{
ResolveInfo info = (ResolveInfo) adapter.getItem(which);
if(info.activityInfo.packageName.contains("facebook"))
{
shareToFacebook();
}
else {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "hello");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "intent");
startActivity(sharingIntent);
}
}

- 59
- 2