gpt4 book ai didi

android - 使用 ACTION_SEND Intent 时过滤掉特定应用程序的异常?

转载 作者:太空狗 更新时间:2023-10-29 14:44:14 27 4
gpt4 key购买 nike

我在按照来自的说明使用 ACTION_SEND 时尝试过滤特定的应用程序 How to filter specific apps for ACTION_SEND intent (and set a different text for each app)

它在我的手机(Android M4 Aqua)上运行完美,但是当我使用 LG G3 手机时,它复制了 gmail,并且没有过滤 Android Beam、Zalo、Blue Mail ...。

如下图所示: Filter List

这是上面链接中的代码:

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 手机上的调试: debug on LG G3

-“intentList”与上面代码中的intentList相同(列表“intentList”)

-“resolveInfoList”是列表“resInfo”

-“列表”只是一个测试列表,我用它来添加发送包名称如您所见,“intentList”仅包含一项(gmail 包项),但 G3 上的过滤器列表显示 6 项:两个 Gmail、Zalo、Android Beam、Email、BlueMail。我无法弄清楚为什么它与 Intent 列表不同。有人可以告诉我代码中有什么问题吗(在我的 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);
}

即使我尝试使用 hashmap 删除标记列表中的重复项目,它也没有用,列表中仍然有两个 gmail 项目。我如何摆脱它?

最佳答案

这是因为Intent.EXTRA_INITIAL_INTENTS标志;此标志告诉选择器添加支持特定 Intent 的应用程序以及初始 Intent 的应用程序。由于 Gmail 应用程序同时支持 emailIntent 和 sendIntent,它在列表中出现了两次。您可以简单地删除 android.gm 部分,因为 Gmail 无论如何都支持 message/rfc822 消息..

更新:由于您为两个 Intent 创建了选择器,因此您必须查询两个 Intent 并仅添加不重复的应用程序,因此首先查询电子邮件 Intent,添加所有应用程序,然后查询 Send Intent 但仅添加它们的应用程序包名称尚未存在。

关于android - 使用 ACTION_SEND Intent 时过滤掉特定应用程序的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42641579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com