Open Google Plus Page Via Intent In Android

前端 未结 6 1249
情书的邮戳
情书的邮戳 2020-12-13 19:53

I have a Google Plus page

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

and an Android application.

相关标签:
6条回答
  • 2020-12-13 20:26
    /**
     * 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;
    }
    
    0 讨论(0)
  • 2020-12-13 20:29

    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.

    0 讨论(0)
  • 2020-12-13 20:41

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-13 20:45

    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.

    0 讨论(0)
  • 2020-12-13 20:46
    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)));
        }
    }
    
    0 讨论(0)
  • 2020-12-13 20:53

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

    0 讨论(0)
提交回复
热议问题