I希望您在使用ACTION_SEND时能够过滤特定的应用程序,按照以下说明进行操作如何为ACTION_SEND意图过滤特定的应用程序(并针对每个应用程序设置不同的文本)。
它在我的手机(Android M4 Aqua)上完美运行,但当我使用LG G3手机时,它会重复发送gmail,而且无法过滤Android Beam,Zalo,Blue Mail等应用程序。
它显示如下图片:过滤列表 以下是来自上述链接的代码:
即使我尝试使用哈希表删除标记列表中的重复项,它仍然没有起作用,仍然有两个Gmail项留在列表中。我该如何摆脱它?
它在我的手机(Android M4 Aqua)上完美运行,但当我使用LG G3手机时,它会重复发送gmail,而且无法过滤Android Beam,Zalo,Blue Mail等应用程序。
它显示如下图片:过滤列表 以下是来自上述链接的代码:
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);
}
以下是我在LG G3手机上的调试结果: LG G3上的调试
“intentList”与上面代码中的列表“intentList”相同。
“resolveInfoList”是列表“resInfo”。
“list”只是一个测试列表,我用它来添加发送方包名。正如你所看到的,“intentList”只包含一个项目(gmail包项),但G3上的过滤器列表显示了6个项目:两个Gmail、Zalo、Android Beam、Email、BlueMail。我无法弄清楚为什么它与意图列表不同。有人能告诉我代码错在哪里吗?(这段代码在我的Android M4 Aqua上完美运行)更新 根据Keivan Esbati的评论,我更新了代码,几乎达到了我想要的效果,但出现了问题,如果我像这样添加gmail:
public void onShareClick() {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_TEXT, "123");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "q23");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, "string");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
}
if (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");
intent.putExtra(Intent.EXTRA_TEXT, shareText.getText()
.toString());
intent.putExtra(Intent.EXTRA_SUBJECT,
getString(R.string.intent_subject));
intentList.add(new LabeledIntent(intent, packageName, ri
.loadLabel(pm), ri.icon));
}
}
在LG G3中应该可以工作,但在Sony M4中只显示默认邮件。如果像下面的代码一样将其删除:
public void onShareClick() {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_TEXT, "123");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "q23");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, "string");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
}
}
//tried to remove duplicated items but it didn't work
Set<LabeledIntent> stringSet=new HashSet<LabeledIntent>();
stringSet.addAll(intentList);
intentList.clear();
intentList.addAll(stringSet);
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
即使我尝试使用哈希表删除标记列表中的重复项,它仍然没有起作用,仍然有两个Gmail项留在列表中。我该如何摆脱它?