Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Facebook Page in Facebook App (if installed) on Android [duplicate]

I would like to open my Facebook page from my Android app, if the Facebook app is available - if not: this page should be open in the default browser.

for this i tried the following code:

try {   Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/MY_PAGE_ID")); startActivity(intent); } catch (Exception e) {    startActivity(new Intent(Intent.ACTION_VIEW,    Uri.parse("http://www.facebook.com/MY_PAGE_NAME"))); } 

Problem is: I have here an Android device with the newest version of Facebook. If I would like to open from my app the Facebook page, the Facebook app will open , but without my page.

I only see the message:

Trouble Loading Timeline.

What is wrong?

like image 290
SpecialFighter Avatar asked Jan 02 '16 08:01

SpecialFighter


People also ask

Why Facebook link open in browser instead of app?

Every android app will have list of urls that it can open. So you have to go to that app settings and tell that it should open in browser for the urls and not in the app. To do that go to Settings -> Apps -> scroll down to the app that you don't want URLs to open in -> Tap on 'Open by Default' and select always Ask.

How do I open Facebook app on Android?

Opening Facebook's official android app from your own app is really easy. You just need to create an intent with correct uri and start and activity with that intent. The app will open with the right page, group, picture, video or etc. this will display option to select a browser or select facebook app.


1 Answers

"fb://page/ does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href= for newer versions.

This is a full fledged working code currently live in one of my apps:

public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName"; public static String FACEBOOK_PAGE_ID = "YourPageName";  //method to get the right URL to use in the intent public String getFacebookPageURL(Context context) {         PackageManager packageManager = context.getPackageManager();         try {             int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;             if (versionCode >= 3002850) { //newer versions of fb app                 return "fb://facewebmodal/f?href=" + FACEBOOK_URL;             } else { //older versions of fb app                 return "fb://page/" + FACEBOOK_PAGE_ID;             }         } catch (PackageManager.NameNotFoundException e) {             return FACEBOOK_URL; //normal web url         }     } 

This method will return the correct url for app if installed or web url if app is not installed.

Then start an intent as follows:

Intent facebookIntent = new Intent(Intent.ACTION_VIEW); String facebookUrl = getFacebookPageURL(this); facebookIntent.setData(Uri.parse(facebookUrl)); startActivity(facebookIntent); 

That's all you need.

like image 158
AndroidMechanic - Viral Patel Avatar answered Sep 24 '22 17:09

AndroidMechanic - Viral Patel