Open Facebook page from Android app?

前端 未结 26 3469
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 07:03

from my Android app, I would like to open a link to a Facebook profile in the official Facebook app (if the app is installed, of course). For iPhone, there exists the

26条回答
  •  执念已碎
    2020-11-22 07:45

    A more reusable approach.

    This is a functionality we generally use in most of our apps. Hence here is a reusable piece of code to achieve this.

    (Similar to other answers in terms for facts. Posting it here just to simplify and make the implementation reusable)

    "fb://page/ does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href= for newer versions. (Like mentioned in another answer here)

    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.

提交回复
热议问题