Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Facebook Url with Facebook application

I want to open an Facebook link from my android application. The URL is looks like http://www.facebbok.com/abcxyz. It should open the 'abcxyz' page in the Facebook application, but it is always opening in browser.

Code:

try 
{
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    activityContext.startActivity(browserIntent);
} 
catch (ActivityNotFoundException ex) 
{
     ex.printStackTrace();
}

My android OS version is 6.0.1.

I have the same issue with Instagram, http://www.instagram.com/abcxyz, while other applications like Youtube work.

like image 220
Raji A C Avatar asked Jan 03 '16 14:01

Raji A C


People also ask

How do I open links on Facebook app instead of Safari?

4 – Tap Settings. 5 – Scroll down and tap Media. 6 – Scroll down top the “Links open externally” setting and check that box. That's all there is to it.


1 Answers

You should use facebook's custom url scheme to force app to open your page like below:

public Intent getFacebookIntent(String url) {

  PackageManager pm = context.getPackageManager();
  Uri uri = Uri.parse(url);

  try {
    ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
    if (applicationInfo.enabled) {
      uri = Uri.parse("fb://facewebmodal/f?href=" + url);
    }
  }  

  catch (PackageManager.NameNotFoundException ignored) {
  }

  return new Intent(Intent.ACTION_VIEW, uri);
}
like image 66
st. Avatar answered Oct 13 '22 15:10

st.