【问题标题】:Open Google Plus Page Via Intent In Android在 Android 中通过 Intent 打开 Google Plus 页面
【发布时间】:2012-08-05 18:25:52
【问题描述】:

我有一个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,则不会崩溃。

【问题讨论】:

  • 如何将 WebView 嵌入到您的应用中并在其中加载页面?
  • 因为我更喜欢直接打开浏览器。
  • Google+ 社区怎么样? stackoverflow.com/questions/23075842/…
  • 现在变成了 com.google.android.libraries.social.gateway.GatewayActivity

标签: android android-intent google-plus


【解决方案1】:
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)));
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案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】:

        您必须首先检查用户是否已经在他/她的手机中安装了 G+ 应用程序?如果是,那么我们可以通过特定意图启动它,或者我们可以使用浏览器重定向到特定页面。

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

        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

        对于推特

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

        【讨论】:

        • 将网关活动更改为:com.google.android.libraries.social.gateway.GatewayActivity
        • 关于“com.google.android.apps.plus.phone.UrlGatewayActivity”,你不应该假设这是活动的路径。相反,通过调用 queryIntentActivities 仅使用 packageName,然后检查其中哪些与应用程序的 packageName 匹配。
        • 对于 fb:网络意图的 url 应更改为“facebook.com/"+facebookId
        • 对于 facebook 应用程序版本 11+ 的意图,请参阅 stackoverflow.com/questions/4810803/… 的 Jareds 回答。网址:fb://facewebmodal/f?href=[NORMAL_FACEBOOK_PAGE_URL]
        【解决方案5】:

        如果用户安装了 Google+ 应用,您可以这样做:

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

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

        【讨论】:

          【解决方案6】:

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

          此外,我不确定这是否会有所不同,但 ID 中有错字。你写道:

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

          但最初的 ID 是101839105638971401281。你最后把 1 去掉了。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-17
            • 2012-07-04
            • 1970-01-01
            • 1970-01-01
            • 2013-09-28
            • 2015-04-02
            相关资源
            最近更新 更多