Bjö*_*lek 154 android facebook url-scheme
从我的Android应用程序,我想在官方Facebook应用程序中打开一个Facebook个人资料的链接(当然,如果安装了该应用程序).对于iPhone,存在fb://
URL方案,但在我的Android设备上尝试相同的操作会引发一个问题ActivityNotFoundException
.
有没有机会通过代码在官方Facebook应用程序中打开Facebook个人资料?
joa*_*gcd 235
这适用于最新版本:
使用此方法:
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>"));
}
}
Run Code Online (Sandbox Code Playgroud)如果用户安装了Facebook应用程序,这将打开它.否则,它将在浏览器中打开Facebook.
编辑:自版本11.0.0.11.23(3002850)Facebook应用程序不再支持这种方式,还有另一种方法,请查看以下来自Jared Rummler的回复.
Jar*_*ler 145
在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);
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ohn 38
这不容易吗?例如在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")));
}
Run Code Online (Sandbox Code Playgroud)
PS.从http://graph.facebook.com/ [userName]获取您的ID(大号)
Mur*_*mel 31
对于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));
}
Run Code Online (Sandbox Code Playgroud)
对于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));
}
Run Code Online (Sandbox Code Playgroud)
...因为没有一个答案指出了差异
两者都在Facebook v.27.0.0.24.15和Nexus 4上的Android 5.0.1上进行了测试
Som*_*123 27
这是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);
Run Code Online (Sandbox Code Playgroud)
MBH*_*MBH 21
这是执行此操作的最简单的代码
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/pages/"+pageid;
intent.setData(Uri.parse(urlBrowser));
}
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
And*_*tel 15
一种更可重用的方法.
这是我们在大多数应用中通常使用的功能.因此,这是一个可重用的代码来实现这一目标.
(类似于事实方面的其他答案.在此发布它只是为了简化并使实现可重用)
"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
}
}
Run Code Online (Sandbox Code Playgroud)
如果安装了app,此方法将返回正确的url url,如果未安装app,则返回web url.
然后按如下方式启动意图:
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
Run Code Online (Sandbox Code Playgroud)
这就是你所需要的.
小智 11
"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
}
}
Run Code Online (Sandbox Code Playgroud)
如果安装了app,此方法将返回正确的url url,如果未安装app,则返回web url.
然后按如下方式启动意图:
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
Run Code Online (Sandbox Code Playgroud)
这是由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);
Run Code Online (Sandbox Code Playgroud)
试试这段代码:
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)));
}
Run Code Online (Sandbox Code Playgroud)
截至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)));
}
}
Run Code Online (Sandbox Code Playgroud)
为此,我们需要“ Facebook页面ID”,您可以获取它:
你可以这样做:
String facebookId = "fb://page/<Facebook Page ID>";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
Run Code Online (Sandbox Code Playgroud)
或者,您可以验证未安装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)));
}
Run Code Online (Sandbox Code Playgroud)
经过多次测试后,我找到了最有效的解决方案之一:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
302032 次 |
最近记录: |