androidandroid-intentgoogle-plus

Use ACTION_VIEW with Google Plus android


I need to create buttons for social sharing in my Android app. I've used ACTION_VIEW to open url with specific text in browser for sharing. Here some examples:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/tweet?text="+trackable));
                startActivity(browserIntent);

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.facebook.com/sharer.php?u="+trackable));
                startActivity(browserIntent); 

How can I do the same interacting with google+ ?


Solution

  • You can use ShareCompat.IntentBuilder from the support library. For instance:

    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
             .setText("Hello Google+!")
             .getIntent()
             .setPackage("com.google.android.apps.plus");
    

    A more elegant and expandable solution (specific to Google+) is to use Google Play Services' PlusShare.Builder (here also supplying a link):

    Intent shareIntent = new PlusShare.Builder(this)
              .setType("text/plain")
              .setText("Hello Google+!")
              .setContentUrl(Uri.parse("https://developers.google.com/+/"))
              .getIntent();
    

    Note that this doesn't open the browser; it will open the Google+ app to allow the user to share comfortably from the app.