比如说,想发送带有附件的邮件,然后打开邮件的客户端,结果弹出一大群的不相关的app,比如蓝牙啊,Skype啊。
直接上代码
Intent iEmail = new Intent(android.content.Intent.ACTION_SEND);
iEmail.setType("message/rfc822");
try {
List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(iEmail, 0);
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
for (ResolveInfo info : resInfo) {
Intent targeted = getEmailIntent();//getEmailIntent()这里是自己需求intent
ActivityInfo activityInfo = info.activityInfo;
if (activityInfo.name.contains("mail")) {
targeted.setPackage(activityInfo.packageName);
targetedShareIntents.add(targeted);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "report attached");
if (chooserIntent == null) {
return;
}
// A Parcelable[] of Intent or LabeledIntent objects as set with
// putExtra(String, Parcelable[]) of additional activities to place
// a the front of the list of choices, when shown to the user with a
// ACTION_CHOOSER.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.e(LOG_TAG, "Unable to email logs " + e.toString());
}