Androidアプリから、公式FacebookアプリのFacebookプロフィールへのリンクを開きたいと思っています(もちろんアプリがインストールされていればの話ですが)。iPhoneでは、fb://
というURLスキームが存在しますが、Android端末で同じことをしようとすると、AttivityNotFoundException
が発生します。
コードから公式FacebookアプリでFacebookのプロフィールを開くことはできないのでしょうか?
最新のバージョンで動作します。
1.https://graph.facebook.com/
public static Intent getOpenFacebookIntent(Context context) {.
試しに
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/<ユーザー名_here>"));
}
}
これは、ユーザーがFacebookアプリをインストールしていれば、そのアプリを開きます。そうでない場合は、ブラウザでFacebookを開きます。
EDIT: バージョン11.0.0.11.23 (3002850)以降、Facebookアプリはこの方法をサポートしなくなりましたが、別の方法があります。
これを行うための最もシンプルなコードは以下の通りです。
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);
}
これは、FrAndroidフォーラムのPierre87氏によってリバースエンジニアリングされていますが]1、公式に記述されているものはどこにも見当たりません。
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);