私の Android アプリから、公式の Facebook アプリで Facebook プロフィールへのリンクを開きたいと思います (もちろんアプリがインストールされている場合)。iPhone には URL スキームが存在しfb://
ますが、私の Android デバイスで同じことをしようとすると、ActivityNotFoundException
.
コードから公式 Facebook アプリで Facebook プロフィールを開く機会はありますか?
私の Android アプリから、公式の Facebook アプリで Facebook プロフィールへのリンクを開きたいと思います (もちろんアプリがインストールされている場合)。iPhone には URL スキームが存在しfb://
ますが、私の Android デバイスで同じことをしようとすると、ActivityNotFoundException
.
コードから公式 Facebook アプリで Facebook プロフィールを開く機会はありますか?
これは最新バージョンで動作します:
次の方法を使用します。
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 アプリが開きます。それ以外の場合は、ブラウザーで Facebook が開きます。
編集: バージョン 11.0.0.11.23 (3002850) 以降、Facebook アプリはこの方法をサポートしなくなりました。別の方法があります。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);
}
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 から送信されたメールがどのようにして 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);
より再利用可能なアプローチ。
これは、ほとんどのアプリで一般的に使用されている機能です。したがって、これを実現するための再利用可能なコードを次に示します。
(事実に関する他の回答と同様です。実装を単純化して再利用可能にするためだけにここに投稿します)
"fb://page/
FB アプリの新しいバージョンでは動作しません。fb://facewebmodal/f?href=
新しいバージョンに使用する必要があります。(ここの別の回答で述べたように)
これは、現在私のアプリの 1 つにある本格的な作業コードです。
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);
それだけです。
これは、FrAndroid フォーラムで Pierre87 によってリバース エンジニアリングされましたが、それについて説明している公式のサイトがどこにも見つからないため、文書化されておらず、いつでも動作を停止する可能性があるものとして扱う必要があります。
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)));
}
多くのテストを行った結果、最も効果的な解決策の 1 つを見つけました。
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 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"));
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;
}
}
定数を宣言する
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);