Open Facebook page from Android app?

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

    This works on the latest version:

    1. Go to https://graph.facebook.com/<user_name_here> (https://graph.facebook.com/fsintents for instance)
    2. Copy your id
    3. Use this method:

      public static Intent getOpenFacebookIntent(Context context) {
      
         try {
          context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
          return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id_here>"));
         } catch (Exception e) {
          return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name_here>"));
         }
      }
      

    This will open the Facebook app if the user has it installed. Otherwise, it will open Facebook in the browser.

    EDIT: since version 11.0.0.11.23 (3002850) Facebook App do not support this way anymore, there's another way, check the response below from Jared Rummler.

    0 讨论(0)
  • 2020-11-22 07:46

    Answering this in October 2018. The working code is the one using the pageID. I just tested it and it is functional.

    public static void openUrl(Context ctx, String url){
        Uri uri = Uri.parse(url);
        if (url.contains(("facebook"))){
            try {
                ApplicationInfo applicationInfo = ctx.getPackageManager().getApplicationInfo("com.facebook.katana", 0);
                if (applicationInfo.enabled) {
                    uri = Uri.parse("fb://page/<page_id>");
                    openURI(ctx, uri);
                    return;
                }
            } catch (PackageManager.NameNotFoundException ignored) {
                openURI(ctx, uri);
                return;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 07:46

    I implemented in this form in webview using fragment- inside oncreate:

     webView.setWebViewClient(new WebViewClient()
    {
        public boolean shouldOverrideUrlLoading(WebView viewx, String urlx)
                    {
         if(Uri.parse(urlx).getHost().endsWith("facebook.com")) {
                            {
                                goToFacebook();
                            }
                            return false;
                        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                        viewx.getContext().startActivity(intent);
                        return true;
                    }
    
    });
    

    and outside onCreateView :

     private void goToFacebook() {
            try {
                String facebookUrl = getFacebookPageURL();
                Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
                facebookIntent.setData(Uri.parse(facebookUrl));
                startActivity(facebookIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //facebook url load
        private String getFacebookPageURL() {
            String FACEBOOK_URL = "https://www.facebook.com/pg/XXpagenameXX/";
            String facebookurl = null;
    
            try {
                PackageManager packageManager = getActivity().getPackageManager();
    
                if (packageManager != null) {
                    Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");
    
                    if (activated != null) {
                        int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
    
                        if (versionCode >= 3002850) {
                            facebookurl = "fb://page/XXXXXXpage_id";
                        }
                    } else {
                        facebookurl = FACEBOOK_URL;
                    }
                } else {
                    facebookurl = FACEBOOK_URL;
                }
            } catch (Exception e) {
                facebookurl = FACEBOOK_URL;
            }
            return facebookurl;
        }
    
    0 讨论(0)
  • 2020-11-22 07:48

    As of July 2018 this works perfectly with or without the Facebook app on all the devices.

    private void goToFacebook() {
        try {
            String facebookUrl = getFacebookPageURL();
            Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
            facebookIntent.setData(Uri.parse(facebookUrl));
            startActivity(facebookIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private String getFacebookPageURL() {
        String FACEBOOK_URL = "https://www.facebook.com/Yourpage-1548219792xxxxxx/";
        String facebookurl = null;
    
        try {
            PackageManager packageManager = getPackageManager();
    
            if (packageManager != null) {
                Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");
    
                if (activated != null) {
                    int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
    
                    if (versionCode >= 3002850) {
                        facebookurl = "fb://page/1548219792xxxxxx";
                    }
                } else {
                    facebookurl = FACEBOOK_URL;
                }
            } else {
                facebookurl = FACEBOOK_URL;
            }
        } catch (Exception e) {
            facebookurl = FACEBOOK_URL;
        }
        return facebookurl;
    }
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:49

    this is the simplest code for doing this

    public final void launchFacebook() {
            final String urlFb = "fb://page/"+yourpageid;
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(urlFb));
    
            // If a Facebook app is installed, use it. Otherwise, launch
            // a browser
            final PackageManager packageManager = getPackageManager();
            List<ResolveInfo> list =
                packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
            if (list.size() == 0) {
                final String urlBrowser = "https://www.facebook.com/"+pageid;
                intent.setData(Uri.parse(urlBrowser));
            }
    
            startActivity(intent);
        }
    
    0 讨论(0)
提交回复
热议问题