Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Created May 28, 2014 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anzfactory/aa6db170a7b11cd3e6ed to your computer and use it in GitHub Desktop.
Save anzfactory/aa6db170a7b11cd3e6ed to your computer and use it in GitHub Desktop.
Facebookの公式アプリを投稿画面にリンク設定した状態で呼び出す方法
private void shareFacebook(String text)
{
String packageName = "com.facebook.katana";
String activityName = null;
// 該当パッケージをもつアプリで、ACTION_SENDに対応しているactivityを取得する
// 暗黙的インテントで表示されるリストから該当のアプリを探し出すという感じ
PackageManager pm = getPackageManager();
Intent intent = new Intent( Intent.ACTION_SEND );
intent.setType("text/plain");
List<ResolveInfo> resolves = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
ActivityInfo activityinfo = null;
for (ResolveInfo info: resolves) {
activityinfo = info.activityInfo;
if (activityinfo.packageName.equals(packageName)) {
activityName = activityinfo.name;
break;
}
Log.d("package name", activityinfo.packageName);
Log.d("activity name", activityinfo.name);
}
// Facebook公式アプリがインストールされてない
if (activityName == null) {
Toast t = Toast.make(getApplicationContext(), "not installed", Toast.LENGTH_LONG);
t.show();
return;
}
// 呼び出す
ComponentName componentName = new ComponentName(packageName, activityName);
intent.setComponent(componentName)
.putExtra(Intent.EXTRA_TEXT, text);
startActivity(intent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment