私の知る限り、StackOverflow にはさまざまな方法でこの質問をする人がたくさんいますが、まだ完全に答えた人はいません。
私の仕様では、ユーザーがメール、Twitter、Facebook、または SMS を選択し、それぞれにカスタム テキストを使用できるようにする必要がありました。これが私がそれを達成した方法です:
public void onShareClick(View v) {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
さまざまな場所でこれを行う方法を少し見つけましたが、他の場所ですべてを 1 か所で見たことはありません。
この方法では、wifi や Bluetooth での共有など、私が望まないばかげたオプションもすべて非表示になることに注意してください。
これが誰かに役立つことを願っています。
編集:
コメントで、このコードが何をしているかを説明するように求められました。基本的にACTION_SEND
は、ネイティブ メール クライアント専用のインテントを作成してから、他のインテントをチューザーに追加します。元のインテントを電子メール固有にすることで、wifi や bluetooth などの余分なジャンクをすべて取り除き、ACTION_SEND
プレーンテキスト タイプのジェネリックから必要な他のインテントを取得し、セレクターを表示する前にそれらを追加します。
追加のインテントを取得したら、それぞれにカスタム テキストを設定します。
Edit2:これを投稿してからしばらく経ちましたが、状況が少し変わりました。オプションのリストに gmail が 2 回表示される場合は、以下の @h_k によるコメントで提案されているように、「android.gm」の特別な処理を削除してみてください。
この 1 つの回答が、私のスタックオーバーフローのレピュテーション ポイントのほぼすべてのソースであるため、少なくとも最新の状態に保つ必要があります。