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

时间:2012-08-08 15:29:09

标签: android android-intent google-plus

我有一个Google Plus页面

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

和一个Android应用程序。 我想在我的应用中打开此页面。我不想打开浏览器!

这将打开浏览器:

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

这次崩溃:

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

提前致谢!

[解决]

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

使用此解决方案,用户可以选择Google Plus APP或打开浏览器。如果选择APP,则不会发生崩溃。

6 个答案:

答案 0 :(得分:25)

如果用户安装了Google+应用,您可以执行以下操作:

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

请注意URI的语法,并且它不包含/b/id/

答案 1 :(得分:21)

您必须先检查用户是否已在手机中安装了G + App?如果是,那么我们可以按特定意图启动它,或者我们可以使用浏览器重定向到特定页面。

这是这种流程中的一种方法,

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

现在你可以简单地调用这个方法,

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

扩展答案您可以使用的Twitter和Facebook的方式相同

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

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

答案 2 :(得分: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;
}

答案 3 :(得分:1)

堆栈跟踪在崩溃时会说什么?

此外,我不确定这是否有所作为,但ID中存在拼写错误。你写道:

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

但最初ID为101839105638971401281。你最后离开了1。

答案 4 :(得分:0)

为什么不只是Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 。 Android OS查询可以处理特定Uri的所有应用程序。 Google+,作为应用程序,已编程为能够处理您要求的Uri。因此,它会在选择器中显示为一个选项(或者如果用户已选择Google+应用作为该Uri的默认设置,则只需选择该选项。

答案 5 :(得分: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)));
    }
}