Open Facebook page from Android app?

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

    I have created a method to open facebook page into facebook app, if app is not existing then opening in chrome

        String socailLink="https://www.facebook.com/kfc";
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String facebookUrl = Utils.getFacebookUrl(getActivity(), socailLink);
        if (facebookUrl == null || facebookUrl.length() == 0) {
            Log.d("facebook Url", " is coming as " + facebookUrl);
            return;
        }
        intent.setData(Uri.parse(facebookUrl));
        startActivity(intent);
    

    Utils.class add these method

    public static String getFacebookUrl(FragmentActivity activity, String facebook_url) {
        if (activity == null || activity.isFinishing()) return null;
    
        PackageManager packageManager = activity.getPackageManager();
        try {
            int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
            if (versionCode >= 3002850) { //newer versions of fb app
                Log.d("facebook api", "new");
                return "fb://facewebmodal/f?href=" + facebook_url;
            } else { //older versions of fb app
                Log.d("facebook api", "old");
                return "fb://page/" + splitUrl(activity, facebook_url);
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.d("facebook api", "exception");
            return facebook_url; //normal web url
        }
    }
    

    and this

     /***
     * this method used to get the facebook profile name only , this method split domain into two part index 0 contains https://www.facebook.com and index 1 contains after / part
     * @param context contain context
     * @param url contains facebook url like https://www.facebook.com/kfc
     * @return if it successfully split then return "kfc"
     *
     * if exception in splitting then return "https://www.facebook.com/kfc"
     *
     */
     public static String splitUrl(Context context, String url) {
        if (context == null) return null;
        Log.d("Split string: ", url + " ");
        try {
            String splittedUrl[] = url.split(".com/");
            Log.d("Split string: ", splittedUrl[1] + " ");
            return splittedUrl.length == 2 ? splittedUrl[1] : url;
        } catch (Exception ex) {
            return url;
        }
    }
    

提交回复
热议问题