Open Facebook page from Android app?

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

    In Facebook version 11.0.0.11.23 (3002850) fb://profile/ and fb://page/ no longer work. I decompiled the Facebook app and found that you can use fb://facewebmodal/f?href=[YOUR_FACEBOOK_PAGE]. Here is the method I have been using in production:

    /**
     * <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
     * default web browser will be used.</p>
     *
     * <p>Example usage:</p>
     *
     * {@code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
     *
     * @param pm
     *     The {@link PackageManager}. You can find this class through {@link
     *     Context#getPackageManager()}.
     * @param url
     *     The full URL to the Facebook page or profile.
     * @return An intent that will open the Facebook page/profile.
     */
    public static Intent newFacebookIntent(PackageManager pm, String url) {
      Uri uri = Uri.parse(url);
      try {
        ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
        if (applicationInfo.enabled) {
          // http://stackoverflow.com/a/24547437/1048340
          uri = Uri.parse("fb://facewebmodal/f?href=" + url);
        }
      } catch (PackageManager.NameNotFoundException ignored) {
      }
      return new Intent(Intent.ACTION_VIEW, uri);
    }
    
    0 讨论(0)
  • 2020-11-22 07:36
    Intent intent = null;
        try {
            getPackageManager().getPackageInfo("com.facebook.katana", 0);
            String url = "https://www.facebook.com/"+idFacebook;
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href="+url));
        } catch (Exception e) {
            // no Facebook app, revert to browser
            String url = "https://facebook.com/"+idFacebook;
            intent = new Intent(Intent.ACTION_VIEW);
            intent .setData(Uri.parse(url));
        }
        this.startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 07:36

    To launch facebook page from your app, let urlString = "fb://page/your_fb_page_id"

    To launch facebook messenger let urlString = "fb-messenger://user/your_fb_page_id"

    FB page id is usually numeric. To get it, goto Find My FB ID input your profile url, something like www.facebook.com/edgedevstudio then click "Find Numberic ID".

    Voila, you now have your fb numeric id. replace "your_fb_page_id" with the generated Numeric ID

     val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlString))
     if (intent.resolveActivity(packageManager) != null) //check if app is available to handle the implicit intent
     startActivity(intent)
    
    0 讨论(0)
  • 2020-11-22 07:40
    fun getOpenFacebookIntent(context: Context, url: String) {
        return try {
            context.packageManager.getPackageInfo("com.facebook.katana", 0)
            context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/$url/")))
        } catch (e: Exception) {
            context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:42

    try this code:

    String facebookUrl = "https://www.facebook.com/<id_here>";
            try {
                int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
                if (versionCode >= 3002850) {
                    Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
                       startActivity(new Intent(Intent.ACTION_VIEW, uri));
                } else {
                    Uri uri = Uri.parse("fb://page/<id_here>");
                    startActivity(new Intent(Intent.ACTION_VIEW, uri));
                }
            } catch (PackageManager.NameNotFoundException e) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
            }
    
    0 讨论(0)
  • 2020-11-22 07:43

    This has been reverse-engineered by Pierre87 on the FrAndroid forum, but I can't find anywhere official that describes it, so it's has to be treated as undocumented and liable to stop working at any moment:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
    intent.putExtra("extra_user_id", "123456789l");
    this.startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题