【问题标题】:Open Facebook page from Android app?从 Android 应用程序打开 Facebook 页面?
【发布时间】:2011-06-16 04:45:03
【问题描述】:

在我的 Android 应用中,我想在官方 Facebook 应用中打开指向 Facebook 个人资料的链接(当然,如果安装了该应用)。对于 iPhone,存在 fb:// URL 方案,但在我的 Android 设备上尝试相同的操作会引发 ActivityNotFoundException

是否有机会通过代码在官方 Facebook 应用中打开 Facebook 个人资料?

【问题讨论】:

  • Facebook 开发者论坛上有一个没有答案的帖子:forum.developers.facebook.net/viewtopic.php?pid=255227(在此处添加它是因为它表明没有明显的答案,也许有一天会在那里得到答案。 ..)
  • 你可以从here看到我的回答。
  • 加载指向我页面的 FACEBOOK 应用程序总是一个很大的困难。除了一直更改路径之外,facebook 支持并没有通过简单的示例代码提供简单的支持。我决定从我的应用程序中删除 Facebook 链接。

标签: android facebook url-scheme


【解决方案1】:

这适用于最新版本:

  1. 转到https://graph.facebook.com/<user_name_here>(例如https://graph.facebook.com/fsintents
  2. 复制你的身份证
  3. 使用这个方法:

    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>"));
       }
    }
    

如果用户安装了 Facebook 应用,这将打开它。否则,它将在浏览器中打开 Facebook。

编辑:由于版本 11.0.0.11.23 (3002850) Facebook 应用不再支持这种方式,还有另一种方式,请查看 Jared Rummler 的以下回复。

【讨论】:

  • 谢谢你,帮了我很大的忙!现在,如果只有一种方法可以为 twitter 做到这一点
  • 有关 iOS 上可用的 url 方案列表,请参阅 wiki.akosma.com/IPhone_URL_Schemes - 其中一些应该也适用于 Android
  • & 这是该问题的答案;使用“页面”而不是个人资料 - stackoverflow.com/a/15872383/1433187
  • 请注意,Facebook 应用 v1.3.2 不支持 fb://profile/,它包含在库存 Nexus One 2.3.6 中
  • DjHacktorReborn 是对的,从 fb v11 开始,fb://profile/ 和 fb://page/ 都不再工作了
【解决方案2】:

在 Facebook 版本 11.0.0.11.23 (3002850) fb://profile/fb://page/ 不再起作用。我反编译了Facebook应用,发现可以使用fb://facewebmodal/f?href=[YOUR_FACEBOOK_PAGE]。这是我在生产中一直使用的方法:

/**
 * <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);
}

【讨论】:

  • @DjHacktorReborn 你不应该再使用 id。只需使用普通链接即可。
  • @DjHacktorReborn 刚刚检查过,在这里工作。在 Facebook 版本 13.0.0.13.14
  • @AbdulWasae 它已在具有数百万下载量的应用程序中运行。如果您有更好的解决方案,请发布它,而不是对可行的解决方案投反对票。
  • 任何人都可以确认,这仍然有效吗? (截至 2019 年 6 月)。我仍然收到ActivityNotFoundException
  • 这会打开 facebook 页面,但与常规的 fb 页面不同,我们看不到 Like 按钮、个人资料和封面照片等。它直接打开联系信息并显示页面中的所有帖子。
【解决方案3】:

这不是更容易吗? 例如在 onClickListener 中?

try {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/426253597411506"));
    startActivity(intent);
} catch(Exception e) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/appetizerandroid")));
}

PS。从 http://graph.facebook.com/[userName]

获取您的 id(大数字)

【讨论】:

  • 我同意,这更好,因为它不对 Facebook 应用程序包名称或意图过滤器做出任何假设。但是,出于同样的原因,情况更糟,因为其他应用程序也可能指定匹配的意图过滤器。这取决于你想要什么。也许添加你做 intent.setPackageName("com.facebook.katana") 以确保没有其他应用程序被调用。
  • 但是它打开浏览器并显示Facebook数据,如何使用Facebook App(调用Facebook应用程序)?
【解决方案4】:

对于 Facebook 页面:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + pageId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + pageId));
}

对于 Facebook 个人资料:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + profileId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + profileId));
}

...因为没有一个答案指出区别

在 Nexus 4 上均使用 Facebook v.27.0.0.24.15 和 Android 5.0.1 进行了测试

【讨论】:

  • 嗨,如何知道个人资料和页面之间的区别?
  • 个人资料适用于个人,而页面适用于公司、地点、品牌。请参阅:facebook.com/help/217671661585622 [标题:主页与个人资料有何不同?]
  • 对于笔记:intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://note/" + facebookNoteId));
  • 要查找公共页面、事件、个人资料的 ID,您可以使用 FindMyFbId
  • 个人资料未在 Facebook 应用中打开。
【解决方案5】:

这是 2016 年的做法。效果很好,而且使用起来非常简单。

我在查看 Facebook 发送的电子邮件如何打开 Facebook 应用程序后发现了这一点。

// e.g. if your URL is https://www.facebook.com/EXAMPLE_PAGE, you should
// put EXAMPLE_PAGE at the end of this URL, after the ?
String YourPageURL = "https://www.facebook.com/n/?YOUR_PAGE_NAME";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(YourPageURL));

startActivity(browserIntent);

【讨论】:

  • 完美运行,即使在 2016 年也是如此。还解决了您从比利时浏览 Facebook 时出现的问题(未登录时禁用 cookie)。
  • 当我使用它时,我引用的 Facebook 页面的标题在 Facebook 应用程序中丢失,例如页面个人资料图片和页面/活动/见解选项卡。
  • 如果您有页面名称(如上所述),则此方法有效,但如果您有数字页面 ID,则此方法无效。
  • 好的,但没有把我带到一个干净的页面,用户可以轻松地对我的页面进行点赞......仍在寻找这个!有什么解决办法吗?
  • 任何人都必须为@Regis_AG 解决问题?
【解决方案6】:

这是最简单的代码

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);
    }

【讨论】:

  • 这不起作用。当我设置链接“facebook.com/pages/"+pageid
  • 省略“pages/”部分->使用“facebook.com/”+pageId
  • 今天还在工作!非常感谢我把这部分“facebook.com/pages/"+pageid;”换成了这个“”facebook.com/"+pageid;”
  • @naitbrahim 感谢您让我们知道它仍然有效!我已经编辑了答案。顺便说一句,答案是在 2013 年发布的 :) 很高兴它帮助了你
【解决方案7】:

一种更可重用的方法。

这是我们在大多数应用程序中通常使用的功能。因此,这里有一段可重用的代码来实现这一点。

(在事实方面与其他答案类似。在此处发布只是为了简化并使实现可重用)

"fb://page/ 不适用于较新版本的 FB 应用程序。对于较新的版本,您应该使用 fb://facewebmodal/f?href=。 (就像这里另一个答案中提到的那样

这是一个完整的工作代码,目前存在于我的一个应用程序中:

public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";

//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
            if (versionCode >= 3002850) { //newer versions of fb app
                return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
            } else { //older versions of fb app
                return "fb://page/" + FACEBOOK_PAGE_ID;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return FACEBOOK_URL; //normal web url
        }
    }

如果已安装,此方法将返回正确的应用 url,如果未安装,则返回 web url。

然后按如下方式启动一个意图:

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

这就是你所需要的。

【讨论】:

  • 它抛出“android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=fb://page/#### }”
【解决方案8】:

这是reverse-engineered by Pierre87 on the FrAndroid forum,但我找不到任何官方描述它的地方,所以它必须被视为无证并且随时可能停止工作:

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);

【讨论】:

  • 是的,解决方案已停止工作。错误是:“java.lang.SecurityException?: Permission Denial: 从 ProcessRecord 开始 Intent { flg=0x10080000 cmp=com.facebook.katana/.ProfileTabHostActivity? (has extras) }?”。 Activity ProfileTabHostActivity 在 AndroidManifest 中没有 android:exported 标志,因此默认情况下 android:exported="false" 用于此 Activity。结果 ProfileTabHostActivity 不能被其他应用程序启动。唯一希望 FB 开发者能在 FB 应用程序的功能版本中解决这个问题。
  • 我认为解决方法是关闭这个“意外 API”。
【解决方案9】:

为此,我们需要“Facebook page id”,你可以得到它:

  • 从页面转到“关于”。
  • 转到“更多信息”部分。

要在指定的个人资料页面上打开 facebook 应用程序,

你可以这样做:

 String facebookId = "fb://page/<Facebook Page ID>";
  startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));

或者您可以在未安装 facebook 应用时验证,然后打开 facebook 网页。

String facebookId = "fb://page/<Facebook Page ID>";
String urlPage = "http://www.facebook.com/mypage";

     try {
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
        } catch (Exception e) {
         Log.e(TAG, "Application not intalled.");
         //Open url web page.
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
        }

【讨论】:

  • 我没有在 about 选项卡中看到这个 Page ID。是私人的吗?
  • @RoCkRoCk 像这样得到它:curl -skA "Mozilla/5.0" https://www.facebook.com/mypage/ | grep -oE "fb://[^\"]+"
【解决方案10】:

试试这个代码:

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)));
        }

【讨论】:

    【解决方案11】:

    截至 2020 年 3 月,一切正常。

    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)));
        }
    }
    

    【讨论】:

    • 我正在寻找最近的答案,谢谢!我还查找了不推荐使用 "fb://page/" 的版本,它是从 2014 年开始的。因此,老实说,我认为不必包含访问链接的早期方式是安全的。
    【解决方案12】:

    经过大量测试,我找到了最有效的解决方案之一:

    private void openFacebookApp() {
        String facebookUrl = "www.facebook.com/XXXXXXXXXX";
        String facebookID = "XXXXXXXXX";
    
        try {
            int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
    
            if(!facebookID.isEmpty()) {
                // open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
                Uri uri = Uri.parse("fb://page/" + facebookID);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            } else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
                // open Facebook app using facebook url
                Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            } else {
                // Facebook is not installed. Open the browser
                Uri uri = Uri.parse(facebookUrl);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
        } catch (PackageManager.NameNotFoundException e) {
            // Facebook is not installed. Open the browser
            Uri uri = Uri.parse(facebookUrl);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    }
    

    【讨论】:

      【解决方案13】:

      我的答案建立在 joaomgcd 得到广泛接受的答案之上。 如果用户安装了 Facebook 但已禁用(例如通过使用 App Quarantine),则此方法将不起作用。 Twitter 应用的 Intent 将被选中,但无法处理它,因为它已被禁用。

      代替:

      context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
      return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
      

      您可以使用以下内容来决定要做什么:

      PackageInfo info = context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
      if(info.applicationInfo.enabled)
          return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
      else
          return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/620681997952698"));
      

      【讨论】:

        【解决方案14】:

        我找到的最佳答案,效果很好。

        只要在浏览器中进入你在Facebook上的页面,右击,点击“查看源代码”,然后找到page_id属性:你必须在这行后面最后一个后面使用page_id——斜线:

        fb://page/pageID
        

        例如:

        Intent facebookAppIntent;
        try {
            facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/1883727135173361"));
            startActivity(facebookAppIntent);
        } catch (ActivityNotFoundException e) {
            facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/CryOut-RadioTv-1883727135173361"));
            startActivity(facebookAppIntent);
        }
        

        【讨论】:

        • 这只对静态应用有意义。
        【解决方案15】:
        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);
        

        【讨论】:

          【解决方案16】:

          要从您的应用启动 facebook 页面,让 urlString = "fb://page/your_fb_page_id"

          要启动 facebook messenger 让 urlString = "fb-messenger://user/your_fb_page_id"

          FB 页面 id 通常是数字。要获取它,请转到Find My FB ID 输入您的个人资料网址,例如www.facebook.com/edgedevstudio,然后单击“查找数字 ID”。

          瞧,你现在有了你的 fb 数字 ID。用生成的数字 ID 替换“your_fb_page_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)
          

          【讨论】:

            【解决方案17】:

            截至 2018 年 7 月,无论是否在所有设备上安装 Facebook 应用,它都能完美运行。

            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;
            }
            

            【讨论】:

            • FB Lite 应用程序有一个单独的包名称,如果您使用这种方式,那么您还应该检查 com.facebook.lite -- 尽管我没有检查 Lite 是否会处理这些 URL
            • @androidguy 我没有 FB Lite。最好知道代码是否适用于它。如果你能以某种方式对其进行测试并将你的发现放在这里,那将有助于其他仍然来到这里的人。欣赏,谢谢
            • @Immy --> url中的Yourpage-1548219792xxxxxx是页面id或者页面url。所以例如:facebook.com/PageName 是我的页面 url,id 类似于 2345676。所以代码中提到的 URL 将是 facebook.com/PageName 或其他?
            • @NeerajDwivedi 您需要两个:URL 中的页面名称和页面 ID。像这样的东西:'String FACEBOOK_URL = "facebook.com/BeanRoaster-1548219792112345/";'
            • @Immy 我已经使用完全相同的代码将 FACEBOOK_URL 替换为“facebook.com/myPageName-myPageID”,并且我得到了一个顶部带有搜索栏的空白页面。我在 Android 9 上使用最新的 Facebook 应用(Facebook 应用版本 - 237.0.0.44.120)
            【解决方案18】:

            查看我打开 Facebook 特定页面的代码:

            //ID initialization
            ImageView facebook = findViewById(R.id.facebookID);
            
            facebook.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
            
                        String facebookId = "fb://page/327031464582675";
                        String urlPage = "http://www.facebook.com/MDSaziburRahmanBD";
            
                        try {
                           startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
                        }catch (Exception e){
                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
                        }
                    }
                });
            

            【讨论】:

              【解决方案19】:

              我已经创建了一种将 facebook 页面打开到 facebook 应用程序的方法,如果应用程序不存在则在 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 添加这些方法

              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
                  }
              }
              

              还有这个

               /***
               * 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;
                  }
              }
              

              【讨论】:

              • 在最新版本中,这实际上将我带到了关于页面,而我正在寻找导航到主页。有什么建议吗?
              • 你找到解决办法了吗??
              【解决方案20】:

              在不使用 facebook sdk 的情况下在按钮单击事件上打开 fb

               Intent FBIntent = new Intent(Intent.ACTION_SEND);
                  FBIntent.setType("text/plain");
                  FBIntent.setPackage("com.facebook.katana");
                  FBIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
                  try {
                      context.startActivity(FBIntent);
                  } catch (android.content.ActivityNotFoundException ex) {
                      Toast.makeText(context, "Facebook have not been installed.", Toast.LENGTH_SHORT).show( );
                  }
              

              【讨论】:

                【解决方案21】:

                1-要获取您的 ID,请转到您的图像配置文件并单击右键并复制链接地址。

                        try {
                                Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("fb://profile/id"));
                                startActivity(intent);
                            } catch(Exception e) {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("facebook url")));
                            }
                        }
                    });
                

                【讨论】:

                  【解决方案22】:

                  声明常量

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

                  声明方法

                  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;
                          }
                      }
                  

                  调用函数

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

                  【讨论】:

                    【解决方案23】:

                    在 2018 年 10 月回答这个问题。工作代码是使用 pageID 的代码。我刚刚测试了它,它可以正常工作。

                    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;
                            }
                        }
                    

                    【讨论】:

                      【解决方案24】:

                      我在 webview 中使用 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;
                                      }
                      
                      });
                      

                      在 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;
                          }
                      

                      【讨论】:

                        【解决方案25】:
                        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)))
                            }
                        }
                        

                        【讨论】:

                          【解决方案26】:

                          答案对我有用,但缺少的一件事是在清单中添加查询,这是我的解决方案:

                          private void openFacebookApp() {
                          String facebookUrl = "www.facebook.com/XXXXXXXXXX";
                          String facebookID = "XXXXXXXXX";
                          
                          try {
                              int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
                          
                              if(!facebookID.isEmpty()) {
                                  // open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
                                  Uri uri = Uri.parse("fb://page/" + facebookID);
                                  startActivity(new Intent(Intent.ACTION_VIEW, uri));
                              } else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
                                  // open Facebook app using facebook url
                                  Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
                                  startActivity(new Intent(Intent.ACTION_VIEW, uri));
                              } else {
                                  // Facebook is not installed. Open the browser
                                  Uri uri = Uri.parse(facebookUrl);
                                  startActivity(new Intent(Intent.ACTION_VIEW, uri));
                              }
                          } catch (PackageManager.NameNotFoundException e) {
                              // Facebook is not installed. Open the browser
                              Uri uri = Uri.parse(facebookUrl);
                              startActivity(new Intent(Intent.ACTION_VIEW, uri));
                          }
                          

                          }

                          并将其添加到应用程序标记之外的清单中

                              <queries>
                              <package android:name="com.google.android.gm" />
                              <package android:name="com.facebook.katana" />
                              <intent>
                                  <action android:name="android.intent.action.VIEW" />
                                  <data android:scheme="https" />
                              </intent>
                              <intent>
                                  <action android:name="android.intent.action.DIAL" />
                                  <data android:scheme="tel" />
                              </intent>
                              <intent>
                                  <action android:name="android.intent.action.SEND" />
                                  <data android:mimeType="*/*" />
                              </intent>
                          </queries>
                          

                          【讨论】:

                            【解决方案27】:

                            您可以检查是否安装了任何 Facebook 应用程序的所有 Facebook 应用程序。 为了支持 OS 级别 11,我们需要在 AndrodiManifest.xml 中添加它以避免找不到包名异常 -

                            <manifest ...
                            <queries>
                                <package android:name="com.facebook.katana" />
                                <package android:name="com.facebook.lite" />
                                <package android:name="com.facebook.android" />
                                <package android:name="com.example.facebook" />
                            </queries>
                             <application .....
                            

                            然后将此方法添加到您的代码中-

                            public static String isFacebookAppInstalled(Context context){
                            
                                    if(context!=null) {
                                        PackageManager pm=context.getPackageManager();
                                        ApplicationInfo applicationInfo;
                            
                                        //First check that if the main app of facebook is installed or not
                                        try {
                                            applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
                                            return applicationInfo.enabled?"com.facebook.katana":"";
                                        } catch (Exception ignored) {
                                        }
                            
                                        //Then check that if the facebook lite is installed or not
                                        try {
                                            applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
                                            return applicationInfo.enabled?"com.facebook.lite":"";
                                        } catch (Exception ignored) {
                                        }
                            
                                        //Then check the other facebook app using different package name is installed or not
                                        try {
                                            applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
                                            return applicationInfo.enabled?"com.facebook.android":"";
                                        } catch (Exception ignored) {
                                        }
                            
                                        try {
                                            applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
                                            return applicationInfo.enabled?"com.example.facebook":"";
                                        } catch (Exception ignored) {
                                        }
                                    }
                                    return "";
                                }
                            

                            然后启动应用程序 -

                            if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
                             /* Facebook App is installed,So launch it. 
                              It will return you installed facebook app's package
                              name which will be useful to launch the app */
                            
                              Uri uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);
                             Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context);
                                            if (intent != null) {
                                                intent.setAction(Intent.ACTION_VIEW);
                                                intent.setData(uri);
                                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                context.startActivity(intent);
                                            }
                                            else {
                                                Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
                                                context.startActivity(intentForOtherApp);
                                            } 
                             }
                            

                            【讨论】:

                            • 不幸的是,通过武士刀拨打电话对我不起作用,总是在我自己的个人资料上打开 facebook,但从来没有在指定的页面上。
                            • @Itapox 我正在对这个主题进行研发,因为我也面临着它。完成后,我将编辑这个问题。谢谢。
                            【解决方案28】:
                            try {
                                   String[] parts = url.split("//www.facebook.com/profile.php?id=");
                                   getPackageManager().getPackageInfo("com.facebook.katana", 0);
                                   startActivity(new Intent (Intent.ACTION_VIEW, Uri.parse(String.format("fb://page/%s", parts[1].trim()))));
                                } catch (Exception e) {
                                   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                                }
                            

                            【讨论】:

                            【解决方案29】:

                            您可以点击以下按钮打开 Facebook 应用程序:-

                            @Override
                            protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                            
                                this.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
                            
                                    @Override
                                    public void onClick(View v) {
                            
                                        startNewActivity("com.facebook.katana");
                                    }
                                });
                            
                            }
                            
                            public void startNewActivity( String packageName)
                            {
                                Intent intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(packageName);
                                if (intent != null)
                                {
                                    // we found the activity
                                    // now start the activity
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                }
                                else
                                {
                                    // bring user to the market
                                    // or let them choose an app?
                                    intent = new Intent(Intent.ACTION_VIEW);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    intent.setData(Uri.parse("market://details?id="+packageName));
                                    startActivity(intent);
                                }
                            }
                            

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 2014-08-22
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2019-09-09
                              • 1970-01-01
                              • 1970-01-01
                              相关资源
                              最近更新 更多