Open Google Plus Page Via Intent In Android

2019-01-22 02:55发布

问题:

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.

回答1:

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/.



回答2:

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);
    }
}


回答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;
}


回答4:

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.



回答5:

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.



回答6:

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)));
    }
}