ChatGPT解决这个技术问题 Extra ChatGPT

从 Android 应用程序打开 Facebook 页面?

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

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

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

D
Daniele Segato

这适用于最新版本:

转到 https://graph.facebook.com/(例如 https://graph.facebook.com/fsintents)复制您的 id 使用此方法: public static Intent getOpenFacebookIntent(Context context) { try { context. getPackageManager().getPackageInfo("com.facebook.katana", 0); return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/")); } catch (Exception e) { return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/")); } }

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

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


谢谢你,对我帮助很大!现在,如果只有一种方法可以为推特做到这一点
有关 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/ 都不再工作了
J
Jared Rummler

在 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 按钮、个人资料和封面照片等。它直接打开联系信息并显示页面中的所有帖子。
B
Björn Marschollek

这不是更容易吗?例如在 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 应用程序(调用 Facebook 应用程序)?
Z
Ziem

对于 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 应用程序中打开。
S
Someguy123

这是 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 吗?
M
MBH

这是执行此操作的最简单代码

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/"+pageid;”改变了这部分“facebook.com/pages/"+pageid;”
@naitbrahim 感谢您让我们知道它仍然有效!我已经编辑了答案。顺便说一句,答案发布于 2013 年 :) 很高兴它帮助了你
V
Viral Patel

一种更可重用的方法。

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

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

"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:未找到处理 Intent { act=android.intent.action.VIEW dat=fb://page/#### } 的活动”
D
Dhanuka Perera

为此,我们需要“Facebook 页面 id”,您可以获取它:

从页面转到“关于”。

转到“更多信息”部分。

https://i.stack.imgur.com/l58y9.png

要在指定的个人资料页面上打开 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)));
        }

我没有从关于标签中看到此 Page ID。是私人的吗?
@RoCkRoCk 得到它:curl -skA "Mozilla/5.0" https://www.facebook.com/mypage/ | grep -oE "fb://[^\"]+"
A
Ahmad Kayyali

这是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”。
R
Ravi Makvana

试试这个代码:

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

S
SpiralDev

截至 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 年开始的。因此,老实说,我认为不必包含访问链接的早期方式是安全的。
J
Javier Amor Penas

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

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

i
igordc

我的答案建立在 joaomgcd 广泛接受的答案之上。如果用户安装了 Facebook,但被禁用(例如通过使用 App Quarantine),此方法将不起作用。将选择 Twitter 应用程序的意图,但由于它被禁用,它将无法处理它。

代替:

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

F
Fabio says Reinstate Monica

我找到的最佳答案,它工作得很好。

只需在浏览器中转到您在 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);
}

这仅对静态应用程序有意义。
T
Tiger developer
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);

E
EdgeDev

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

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

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

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

I
Immy

截至 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)
P
Papel

查看我打开 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)));
            }
        }
    });

G
Gk Mohammad Emon

您可以检查是否安装了任何 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 我正在对这个主题进行研发,因为我也面临着它。完成后,我将编辑这个问题。谢谢你。
A
Abdul Rizwan

我创建了一种将 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;
    }
}

在最新版本中,这实际上将我带到了关于页面,而我正在寻找导航到主页。有小费吗 ?
你找到解决办法了吗??
D
Drashti Vyas

在不使用 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( );
    }

m
mohamed gemy

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

A
Abhishek Agarwal

声明常量

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

f
franzesz

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

t
thinktechtv

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

H
Hardik Patel
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)))
    }
}

D
Dharman

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

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>

H
Haris Khan

要从您的 Webview 在 Facebook 应用程序中打开 Facebook 页面,请将此代码添加到您的 shouldOverrideUrlLoading 方法中

 if (url.startsWith("https://www.facebook.com || Your Facebook page address")) {
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + url)));
                } catch (Exception e2) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                }
                return true;
            }

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

请发布您的代码的解释。请参阅How to Answer
D
Deepak Sharma

您可以点击按钮打开 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);
    }
}