LoginSignup
6
3

More than 5 years have passed since last update.

Facebookのsdkを使わずにURL共有/ページを起動する

Posted at

初めてAndroidアプリからFacebookに共有する実装をしたので記録しておく。

URLをFacebookに共有する

urlのみであればsdkは不要だが、url以外を共有したい場合はFacebookのsdkが必要になる。

String url = "https://www.google.com";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, url);
intent.setPackage("com.facebook.katana");
startActivity(intent);

画面

スクリーンショット 2017-07-13 23.15.04.png

facebookアプリが入ってなければExceptionが飛んできてアプリが落ちるので、アプリが存在するか事前チェックが必要。

ちなみに、
"Googleだよ https://www.google.com";
とurl以外を埋め込んでもキッチリurlのみ共有する動作になった。

Facebookの特定ページを開く

Facebookの企業ページを開きたい場合もsdkは不要だった。

String url = "https://www.facebook.com/GoogleJapan";
String uriString = "fb://facewebmodal/f?href=" + url;
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(uriString)));

画像

スクリーンショット 2017-07-13 23.15.23.png

こちらもfacebookアプリが入ってなければExceptionとなるので、アプリが存在するか事前チェックが必要。

Facebookアプリのバージョンについて

Facebookアプリのバージョンが11.0.0.11.23(3002850)未満と古い場合は、
uriString = "fb://page/GoogleJapan"としなければならないらしい。

2017/07/13時点のFacebookアプリバージョンは132.0.0.20.85(64682900)だった。

11.0.0.11.23(3002850)って相当古いんだけど、そんな化石バージョンな人いるんかいな...
と思ったけど、アプリをスマフォ購入から一度も更新してないという強者が知人にいたから、バージョン判定は必要なのかもしれない。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3