Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open page in Facebook,Twitter and Google Plus app from other app - Android

I'm working on an application where I need to integrate the social functionality of the different social networks: Facebook, Twitter, Google+.

For now, in Facebook and Twitter i'm recognized if the user has a native application and if he does, I'm opening it and show him my fan page.

For Twitter I use the next code:

try {

  Intent intent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("twitter://user?screen_name=[user_name]"));
    startActivity(intent);

    }catch (Exception e) {
        startActivity(new Intent(Intent.ACTION_VIEW,
             Uri.parse("https://twitter.com/#!/[user_name]"))); 
    } 

And for Facebook the next code:

try{

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + PROFILE_FACEBOOK_APP_ID));
startActivity(intent);

}catch(Exception e){

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/UserNamePage")));
}

Now I want to do the same thing for Google+. I saw that I can browse to my fan page with the next Url https://plus.google.com/MY_PAGE_ID/, but it keep asking me if I want to open it with Google+ application or with the browser, and I want that he will open it with the application automatically, without asking the user.

Is there a simple way to do this? Thanks.

like image 816
Ofir A. Avatar asked Jul 04 '12 11:07

Ofir A.


3 Answers

Found a solution:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.plus",
"com.google.android.apps.plus.phone.UrlGatewayActivity");
intent.putExtra("customAppUri", "FAN_PAGE_ID");
startActivity(intent);
like image 173
Ofir A. Avatar answered Nov 07 '22 07:11

Ofir A.


I think this is quite safe, because we do not need to specify the component, just the google+ app package name:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://plus.google.com/[Google+ID]/"));
intent.setPackage("com.google.android.apps.plus"); // don't open the browser, make sure it opens in Google+ app
startActivity(intent);
like image 7
Randy Sugianto 'Yuku' Avatar answered Nov 07 '22 09:11

Randy Sugianto 'Yuku'


Unknown if google plus needs some other information in the Intent but as general Android solution you can explicitly set the target. You will need the package name of google+.

More info here: http://developer.android.com/reference/android/content/Intent.html#setPackage%28java.lang.String%29

For example:

Intent.setPackage("com.google.android.apps.plus"); //Don't know the exact package name
like image 2
RvdK Avatar answered Nov 07 '22 07:11

RvdK