Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post different text on facebook and twitter using Android intents

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.

like image 764
Amol Gupta Avatar asked Oct 14 '11 07:10

Amol Gupta


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

like image 144
Kurtis Nusbaum Avatar answered Sep 23 '22 15:09

Kurtis Nusbaum