0

Is there an easy way to share a URL & prefilled text to Google+, without adding Google Play Services dependency, using only plain Android intents?

I'm looking for something similar as this solution for Twitter and this one for Facebook.

I'm asking because:

  • I'm not super happy to add new dependencies (even if just play-services-plus) for what is very minor functionality in this app.
  • On my Nexus 5 test device running Android 5.0, Play Services crashes every time I try to share something ("Google Play Services has stopped"), even when I'm using their exact PlusShare.Builder prefill example code. (On another device running Android 4.4.4 it does work.)
Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372

1 Answers1

2

Well, it turned out to be easy using just Intent.ACTION_SEND, putting the whole share text (including URL) in Intent.EXTRA_TEXT:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, 
    "Just testing, check this out: https://stackoverflow.com/questions/28212490/");
filterByPackageName(context, intent, "com.google.android.apps.plus");
context.startActivity(intent);

... where filterByPackageName() is a utility like this (which I've posted before):

public static void filterByPackageName(Context context, Intent intent, String prefix) {
    List<ResolveInfo> matches = context.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().startsWith(prefix)) {
            intent.setPackage(info.activityInfo.packageName);
            return;
        }
    }
}

If you are doing this from an Activity where you used to use the Google PlusShare.Builder API, you can still call activity.startActivityForResult and handle onActivityResult exactly the same way as you did before.

One downside of using this manually constructed Intent to call the Plus app, compared to using the PlusShare.Builder, is that the user will have to pick the Google Plus account (if he has multiple) he wants to share with, regardless of whether you already logged him into your app using Google.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372