Android share intent chooser
I am using something like this to share some text using available applications on the user's phone.
public void share(String subject,String text) {
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
st开发者_运维问答artActivity(Intent.createChooser(intent, getString(R.string.share)));
}
My main problem is that I would like to have a different text if the user chooses Twitter instead of email for example (short version with short URLs VS full text with attached images).
How can ont find out which application the user has decided to use?
Once you hand the text off to the system with createChooser its out of your hands, no way to change the text after that.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, textWhichYouWantToShare);
startActivity(Intent.createChooser(intent, getString(R.string.share)));
On click Social(ex-twitter)
shareOnSocial(activity, h.shareTwitter, Global.TWITTER_ID, shareContent, activity.getResources().getString(R.string.error_twitter));
On Create Chooser
shareOnSocial(activity, h.shareChooser, "choose", shareContent, activity.getResources().getString(R.string.error));
Method share
private void shareOnSocial(Activity mAct, View shareView, String packageId, String content, String error) {
shareView.startAnimation(clickAnimation);
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, content);
i.setType("text/plain");
if (!packageId.equals("choose")) {
i.setPackage(packageId);
try {
mAct.startActivity(i);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(mAct, error, Toast.LENGTH_SHORT).show();
}
} else {
mAct.startActivity(Intent.createChooser(i, mAct.getString(R.string.share)));
}
}
精彩评论