A mia conoscenza, StackOverflow ha molte persone che fanno questa domanda in vari modi, ma nessuno ha ancora risposto completamente.
La mia specifica richiedeva che l'utente potesse scegliere email, twitter, facebook o SMS, con testo personalizzato per ognuno. Ecco come ho compiuto che:
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);
}
Ho trovato pezzi di come fare questo in vari luoghi, ma non ho visto tutto questo in un unico luogo altrove.
Si noti che questo metodo nasconde anche tutte le opzioni stupide che non desidero, come la condivisione su wifi e bluetooth.
Spero che questo aiuti qualcuno.
Modifica: In un commento, mi è stato chiesto di spiegare cosa sta facendo questo codice. Fondamentalmente, sta creando un intento ACTION_SEND per il client di posta nativa SOLO, quindi aggiungendo altri intenti al selettore. Rendere l'intento originale specifico per l'e-mail elimina tutte le cianfrusaglie extra come il wifi e il bluetooth, quindi afferro gli altri intenti che voglio da un generico ACTION_SEND di tipo plaintext e li applico prima di mostrare il selettore.
Quando seleziono gli intenti aggiuntivi, imposto il testo personalizzato per ognuno.
Edit2: È passato un po 'di tempo da quando l'ho pubblicato e le cose sono cambiate un po'. Se visualizzi due volte gmail nell'elenco delle opzioni, prova a rimuovere la gestione speciale per "android.gm" come suggerito in un commento di @h_k qui sotto.
Dal momento che questo risposta è la fonte di quasi tutti i miei punti StackOverflow reputazione, devo almeno provare a tenerlo aggiornato :)
Eventuali duplicati: [custom-filtering-of-intent-chooser-based-on-installato-android-package-name] [1] [1]: http: // StackOverflow .com/questions/5734678/custom-filtering-of-intent-chooser-based-on-installed-android-nome-pacchetto –
Questo blogpost sembra essere la risposta perfetta: http://hkdevtips.blogspot.com/2013/ 02/customize-your-actionchooser-intent.html –
ehi amico ...quando clicco sul pulsante di invio, apro la finestra di dialogo di condivisione e l'elenco di dialoghi condivisi è "gmail, email, zapiya, collegamento" ecc. ma non mostra facebook, whatsapp, facebook messanger, luoghi di ritrovo ... come posso mostrare il suo ?? –