15

I am using something like this to share some text using available applications on the user's phone.

public void share(String subject,String text) {
     final Intent intent = new Intent(Intent.ACTION_SEND);

     intent.setType("text/plain");
     intent.putExtra(Intent.EXTRA_SUBJECT, subject);
     intent.putExtra(Intent.EXTRA_TEXT, text);

     startActivity(Intent.createChooser(intent, getString(R.string.share)));
}

My main problem is that I would like to have a different text if the user chooses Twitter instead of email for example (short version with short URLs VS full text with attached images).

How can ont find out which application the user has decided to use?

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • gumbercules has a fantastic answer here: http://stackoverflow.com/questions/5734678/custom-filtering-of-intent-chooser-based-on-installed-android-package-name – hitch45 May 31 '12 at 22:18

3 Answers3

8

Once you hand the text off to the system with createChooser its out of your hands, no way to change the text after that.

Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • So I'd have to build my own chooser? :-/ Clumsy API design isn't it? In that case, any sample code on how to create the intent specifically for Twitter or Facebook for instance? – Vincent Mimoun-Prat Dec 13 '10 at 17:11
  • 2
    the app in question would have to have published its own public api for sending tweets, update status, etc. Twitdroid is the only app I know of that has done so and no others follow its convention. I personally don't mind this design I think the convenience is worth the trade-off. – Nathan Schwermann Dec 13 '10 at 17:13
  • It is indeed convenient, however it is rather hard to adapt to constraints on message length for instance. I might just add a dialog that asks the user if he want to share the thing as a short or a long text. Not optimal as far as the number of clicks to achieve what you need but only way I can think of so far. Thanks for the input. – Vincent Mimoun-Prat Dec 14 '10 at 12:40
  • 3
    You can specify the package for an intent, but I would advice against that. Instead make your own chooser which has a few special cases for some services (Twitter). Even better would be to make your own chooser with a checkbox for "Shorter text" or something. – alexanderblom Jul 28 '11 at 08:41
0
 Intent intent = new Intent(Intent.ACTION_SEND);

 intent.setType("text/plain");
 intent.putExtra(Intent.EXTRA_SUBJECT, subject);
 intent.putExtra(Intent.EXTRA_TEXT, textWhichYouWantToShare);

 startActivity(Intent.createChooser(intent, getString(R.string.share)));
Vaishali Sutariya
  • 5,093
  • 30
  • 32
0

On click Social(ex-twitter)

shareOnSocial(activity, h.shareTwitter, Global.TWITTER_ID, shareContent, activity.getResources().getString(R.string.error_twitter));

On Create Chooser

shareOnSocial(activity, h.shareChooser, "choose", shareContent, activity.getResources().getString(R.string.error));

Method share

private void shareOnSocial(Activity mAct, View shareView, String packageId, String content, String error) {
    shareView.startAnimation(clickAnimation);
    Intent i = new Intent();
    i.setAction(Intent.ACTION_SEND);
    i.putExtra(Intent.EXTRA_TEXT, content);
    i.setType("text/plain");

    if (!packageId.equals("choose")) {
        i.setPackage(packageId);
        try {
            mAct.startActivity(i);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(mAct, error, Toast.LENGTH_SHORT).show();
        }
    } else {
        mAct.startActivity(Intent.createChooser(i, mAct.getString(R.string.share)));
    }
}
Kumar Santanu
  • 603
  • 1
  • 7
  • 14