Open Facebook page from Android app?

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

    Declare constants

      private String FACEBOOK_URL="https://www.facebook.com/approids";
        private String FACEBOOK_PAGE_ID="approids";
    

    Declare Method

    public String getFacebookPageURL(Context context) {
            PackageManager packageManager = context.getPackageManager();
            try {
                int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
    
                boolean activated =  packageManager.getApplicationInfo("com.facebook.katana", 0).enabled;
                if(activated){
                    if ((versionCode >= 3002850)) {
                        Log.d("main","fb first url");
                        return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
                    } else {
                        return "fb://page/" + FACEBOOK_PAGE_ID;
                    }
                }else{
                    return FACEBOOK_URL;
                }
            } catch (PackageManager.NameNotFoundException e) {
                return FACEBOOK_URL;
            }
        }
    

    Call Function

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

提交回复
热议问题