Jonik
Jonik

Reputation: 81751

Sharing to Google+ with plain intents (without PlusShare / Play Services)

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:

Upvotes: 0

Views: 831

Answers (1)

Jonik
Jonik

Reputation: 81751

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.

Upvotes: 2

Related Questions