【问题标题】:Open Facebook post in Facebook app在 Facebook 应用中打开 Facebook 帖子
【发布时间】:2018-04-19 03:43:30
【问题描述】:

我想在我的应用程序中包含 Facebook 故事,这样当用户点击帖子时,它会在 Facebook 的本地应用程序中打开(如果存在的话)。 我已经尝试了下面的代码,但它似乎已经过时了

 Intent viewIntent;
        try {
            getActivity().getPackageManager().getPackageInfo("com.facebook.katana", 0);
            String uri = "fb://post/" + post.getId();
            viewIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(uri));
        } catch (Exception e) {
            String url = "https://www.facebook.com/" + PAGE_ID
                    + "/posts/" + post.getId;
            viewIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(url));
        }
        startActivity(viewIntent);

【问题讨论】:

    标签: android facebook


    【解决方案1】:

    您使用的 uri 仅适用于旧版本的 facebook 应用程序。使用

    public static String FACEBOOK_URL = "https://www.facebook.com/" + PAGE_ID
                    + "/posts/" + post.getId;
    
    //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://post/" + post.getId();;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return FACEBOOK_URL; //normal web url
        }
    }
    

    然后

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

    这就是你所需要的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2023-03-05
      • 1970-01-01
      • 2020-02-24
      • 2022-01-08
      • 2011-12-14
      相关资源
      最近更新 更多