简体   繁体   English

通过Intent在Android中打开Goog​​le Plus页面

[英]Open Google Plus Page Via Intent In Android

I have a Google Plus page 我有一个Google Plus页面

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

and an Android application. 和一个Android应用程序。 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. 使用此解决方案,用户可以选择Google Plus APP或打开浏览器。 If the APP is chosen, there is no crash. 如果选择APP,则不会发生崩溃。

If the user has the Google+ app installed, you can do this: 如果用户安装了Google+应用,您可以执行以下操作:

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/ . 请注意URI的语法,并且它不包含/b/id/

You have to first check that user already has G+ App in his/her phone or not ? 你必须首先检查用户是否已经在他/她的手机中有G + App? 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 , 扩展答案您可以使用的Twitter和Facebook的方式相同

For Twitter , 对于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 , 对于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);
    }
}
/**
 * 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;
}

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. 此外,我不确定这是否有所作为,但ID中有错字。 You wrote: 你写了:

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

but originally the ID was 101839105638971401281 . 但最初的ID是101839105638971401281 You left off the 1 at the end. 你最后离开了1。

Why not just Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 为什么不只是Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); . Android OS queries all Applications that can handle a specific Uri. Android OS查询可以处理特定Uri的所有应用程序。 Google+, as an app, is programmed to be able to handle the Uri you are requesting. Google+,作为应用程序,已编程为能够处理您要求的Uri。 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. 因此,它会在选择器中显示为一个选项(或者如果用户已经选择Google+应用程序作为该Uri的默认设置,则可以选择它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM