Open Facebook page from Android app?

前端 未结 26 3111
被撕碎了的回忆
被撕碎了的回忆 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

    As of March 2020 this works perfectly.

    private void openFacebookPage(String pageId) {
        String pageUrl = "https://www.facebook.com/" + pageId;
    
        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.katana", 0);
    
            if (applicationInfo.enabled) {
                int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
                String url;
    
                if (versionCode >= 3002850) {
                    url = "fb://facewebmodal/f?href=" + pageUrl;
                } else {
                    url = "fb://page/" + pageId;
                }
    
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            } else {
                throw new Exception("Facebook is disabled");
            }
        } catch (Exception e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(pageUrl)));
        }
    }
    

提交回复
热议问题