21

I have a Google Plus page

https://plus.google.com/u/0/b/101839105638971401281/101839105638971401281/posts

and an Android application. I want to open this page in my app. I don't want to open the browser!

This opens the browser:

URL="https://plus.google.com/b/101839105638971401281/101839105638971401281/posts";
uri = Uri.parse(URL);
it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

this crashes:

 Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setClassName("com.google.android.apps.plus",             "com.google.android.apps.plus.phone.UrlGatewayActivity");

intent.putExtra("customAppUri", "10183910563897140128");
startActivity(intent);

Thanks in advance!

[SOLVED]

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/101839105638971401281/posts")));

With this solution the user can choose the Google Plus APP or open the browser. If the APP is chosen, there is no crash.

Dimitar
  • 4,402
  • 4
  • 31
  • 47
benoffi7
  • 3,068
  • 3
  • 29
  • 44

6 Answers6

24

If the user has the Google+ app installed, you can do this:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/101839105638971401281/posts")));

Notice the syntax of the URI, and that it doesn't contain /b/id/.

Chirag Shah
  • 3,654
  • 22
  • 29
21

You have to first check that user already has G+ App in his/her phone or not ? If yes then we can start it by specific intent or we can use browser redirection to specific page.

Here's one method in such flow,

public void openGPlus(String profile) {
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setClassName("com.google.android.apps.plus",
          "com.google.android.apps.plus.phone.UrlGatewayActivity");
        intent.putExtra("customAppUri", profile);
        startActivity(intent);
    } catch(ActivityNotFoundException e) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/"+profile+"/posts")));
    }
}

Now you can call this method simply like,

//117004778634926368759 is my google plus id
openGPlus("117004778634926368759");

Extended Answer : Same way for twitter and facebook you can use,

For Twitter,

public void openTwtr(String twtrName) {
        try {
           startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("twitter://user?screen_name=" + twtrName)));
        } catch (ActivityNotFoundException e) {
           startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://twitter.com/#!/" + twtrName)));
        }
}

And For Facebook,

public void openFB(String facebookId) {
    try{
        String facebookScheme = "fb://profile/" + facebookId;
        Intent facebookIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme));
        startActivity(facebookIntent);
    } catch (ActivityNotFoundException e) {
        Intent facebookIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.facebook.com/profile.php?id="+facebookId));
        startActivity(facebookIntent);
    }
}
Hardik Thaker
  • 3,050
  • 1
  • 27
  • 37
  • 2
    Change the gateway activity to: com.google.android.libraries.social.gateway.GatewayActivity – Amt87 Jun 07 '15 at 12:02
  • About "com.google.android.apps.plus.phone.UrlGatewayActivity", you should never assume this is the path to the activity. Instead, use only the packageName, by calling queryIntentActivities, and then check which of them matches the packageName of the app. – android developer Dec 17 '15 at 13:15
  • For fb: the url for the web intent should be changed to just "https://www.facebook.com/"+facebookId – Erik Melkersson Jan 26 '16 at 12:21
  • For the facebook app version 11+ intent see Jareds answer at http://stackoverflow.com/questions/4810803/open-facebook-page-from-android-app. Url: fb://facewebmodal/f?href=[NORMAL_FACEBOOK_PAGE_URL] – Erik Melkersson Jan 26 '16 at 12:22
3
/**
 * Intent to open the official Google+ app to the user's profile. If the Google+ app is not
 * installed then the Web Browser will be used.
 * 
 * </br></br>Example usage:</br>
 * <code>newGooglePlusIntent(context.getPackageManager(), "https://plus.google.com/+JaredRummler");</code>
 * 
 * @param pm
 *            The {@link PackageManager}.
 * @param url
 *            The URL to the user's Google+ profile.
 * @return The intent to open the Google+ app to the user's profile.
 */
public static Intent newGooglePlusIntent(PackageManager pm, String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    try {
        if (pm.getPackageInfo("com.google.android.apps.plus", 0) != null) {
            intent.setPackage("com.google.android.apps.plus");
        }
    } catch (NameNotFoundException e) {
    }
    return intent;
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
1

What does the stack trace say when it crashes?

Also I'm not sure if this would make a difference but there's a typo in the ID. You wrote:

intent.putExtra("customAppUri", "10183910563897140128");

but originally the ID was 101839105638971401281. You left off the 1 at the end.

Ken Fehling
  • 2,051
  • 23
  • 32
0

Why not just Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); . Android OS queries all Applications that can handle a specific Uri. Google+, as an app, is programmed to be able to handle the Uri you are requesting. So it will show up as an option in a chooser (or just go to it if the user has already selected the Google+ app to be default for that Uri.

astryk
  • 1,256
  • 13
  • 20
0
public void openTwitter(String twitterName) {
    try {
       startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("twitter://user?screen_name=" + twitterName)));
    } catch (ActivityNotFoundException e) {
       startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://twitter.com/#!/" + twitterName)));
    }
}
Roger Alien
  • 3,040
  • 1
  • 36
  • 46
Ahmed Sayed
  • 155
  • 1
  • 2