从我的Android应用程序中,我想在官方Facebook应用程序中打开一个指向Facebook个人资料的链接(当然,如果已安装该应用程序)。对于iPhone,存在fb://
URL方案,但是在我的Android设备上尝试相同的操作会抛出ActivityNotFoundException
。
是否有可能通过代码在官方Facebook应用程序中打开Facebook个人资料?
从我的Android应用程序中,我想在官方Facebook应用程序中打开一个指向Facebook个人资料的链接(当然,如果已安装该应用程序)。对于iPhone,存在fb://
URL方案,但是在我的Android设备上尝试相同的操作会抛出ActivityNotFoundException
。
是否有可能通过代码在官方Facebook应用程序中打开Facebook个人资料?
Answers:
在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);
}
ActivityNotFoundException
这适用于最新版本:
使用此方法:
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的回复。
这难道不是很容易吗?例如在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页面:
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进行了测试
这是在2016年做到的方式,效果很好,而且非常简单。
在研究了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);
这是执行此操作的最简单的代码
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);
}
一种更可重用的方法。
这是我们大多数应用程序通常使用的功能。因此,这是实现此目的的可重用代码段。
(在事实方面与其他答案类似。将其发布在此处只是为了简化并使实现可重用)
"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);
这就是您所需要的。
这已经被反向工程通过Pierre87在FrAndroid论坛,但我无法找到任何地方的官方描述它,所以它必须被视为无证和容易停止在任何时刻工作:
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);
试试这个代码:
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)));
}
为此,我们需要“ Facebook页面ID”,您可以获取它:
你可以这样做:
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(url)));
}
Page ID
“关于”标签中没有看到此内容。是私人的吗?
经过大量测试,我发现了最有效的解决方案之一:
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));
}
}
我的答案建立在joaomgcd广泛接受的答案之上。如果用户已安装Facebook但被禁用(例如通过使用App隔离),则此方法将不起作用。将选择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"));
我发现的最佳答案,效果很好。
只需在浏览器中的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);
}
截至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)));
}
}
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);
截至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;
}
我已经创建了一种方法来打开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;
}
}
要从您的应用启动Facebook页面,请让urlString =“ fb:// page / your_fb_page_id”
要启动Facebook Messenger,请让urlString =“ fb-messenger:// user / your_fb_page_id”
FB页面ID通常是数字。要获取它,请转到“ 查找我的FB ID”,输入您的个人资料网址,例如www.facebook.com/edgedevstudio,然后单击“查找数字ID”。
瞧,您现在有了fb数字编号。用生成的数字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)
声明常量
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);
在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;
}
}
我在webview中使用片段在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;
}
在不使用facebook sdk的情况下打开fb on button click事件
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( );
}
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)))
}
}
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)));
}
您可以按以下按钮打开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);
}
}