Search code examples
androidandroid-intentsharingandroid-sharing

Android sharing options


I'm writing an app with sharing functionality and I want to have a button to share via a specific social media ie. Facebook, without having to open a chooser. Is there anyway to do this? I've been having a look through the source code to see if I can find anything but so far I can't find a way to do this.

Ideally what I would like to do is call the method that the chooser does to get the list of applicable apps to share with and then display a button for the main social media platforms I am targeting. And then when a user clicks one of the buttons it brings them straight to the app with the media loaded so all they have to do is click post to share.

I know using a chooser isn't rocket science but I want the app to be as simple as possible so removing the chooser would be a good step towards it as it would remove a lot of the unnecessary options like email and some of the things that come with the phone that people don't tend use. Although it is likely that I will have the chooser anyway just incase the user wants to share it some other way too, I would like it not to be the only way to share.

A full solution would be awesome but a point in the right direction in terms of where to look in the source code would be great too.


Solution

  • I haven't tested this but have you tried something like this:

    Intent fb = new Intent(Intent.ACTION_SEND);
    fb.sharingIntent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity");
    fb.putExtra(Intent.EXTRA_TEXT, "message");
    
    Intent twitter = new Intent(Intent.ACTION_SEND);
    twitter.setClassName("com.twitter.android","com.twitter.android.PostActivity");
    twitter.putExtra(Intent.EXTRA_TEXT, "message");
    
    Intent intent = Intent.createChooser(fb, "Share");
    intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {
        twitter, ....more here
    });
    startActivity(intent);
    

    Base on this answer: How to force Share Intent to open a specific app?

    Since these activities can change try the url as well or whatever is documented in their website so it fallbacks to their android intents if installed.