简体   繁体   中英

Open a Facebook post URL in android Facebook app programmatically

I'm coding an android - java app which can open a Facebook post url (ex: https://www.facebook.com/BaHangXom.0/videos/2377836809193490 ). I tried some ways to start Facebook app with intent but unsuccessfully.

    Intent intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + "https://www.facebook.com/BaHangXom.0/videos/2377836809193490"));
    intent.setPackage("com.facebook.katana");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

Could you please give me any idea? Thank you so much.

Simply open the url:

Intent intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/BaHangXom.0/videos/2377836809193490"));
startActivity(intent);

This is the most simple and universal way: user will be able to choose (and remember the choice) whether they want to open the link via main Facebook app, Facebook lite, or the browser.

You can open the facebook app using this code:

public static Intent newFacebookIntent(PackageManager pm, String url) {
  Uri uri = Uri.parse(url);
  try {
    ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
    if (applicationInfo.enabled) {
      // http://stackoverflow.com/a/24547437/1048340
      uri = Uri.parse("fb://facewebmodal/f?href=" + url);
    }
  } catch (PackageManager.NameNotFoundException ignored) {
  }
  return new Intent(Intent.ACTION_VIEW, uri);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM