Android - 如何从电子邮件选择器列表中排除 PayPal?

Posted

技术标签:

【中文标题】Android - 如何从电子邮件选择器列表中排除 PayPal?【英文标题】:Android - How to exclude PayPal from Email chooser list? 【发布时间】:2020-09-27 00:45:24 【问题描述】:

我正在使用此代码通过意图打开电子邮件选择器

val mIntent = Intent(
    Intent.ACTION_SENDTO, Uri.fromParts(
        "mailto",
        "name@email.com", null
    )
)
mIntent.putExtra(Intent.EXTRA_SUBJECT, "[Feedback - App Name]")
mIntent.putExtra(Intent.EXTRA_TEXT, "")
try 
    startActivity(Intent.createChooser(mIntent, "Choose Email Client..."))
 catch (e: Exception) 
    (activity as MainActivity).showToast("There are no email clients installed.")

它按预期工作,但它还在电子邮件客户端列表中包含 PayPal 应用程序。

点击 PayPal 后,如果电子邮件已在 PayPal 上注册,它会重定向到付款页面。如果未注册,则显示在屏幕下方。

在这两种情况下,都没有从 PayPal 应用发送电子邮件的选项。

那么,我如何才能在意图选择器中仅包含电子邮件客户端?

我也试过这段代码,结果也是一样的。

val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:")
intent.putExtra(Intent.EXTRA_EMAIL, "addresses")
intent.putExtra(Intent.EXTRA_SUBJECT, "subject")
if (intent.resolveActivity(activity?.getPackageManager()!!) != null) 
    startActivity(Intent.createChooser(intent, "Choose Email Client..."))


编辑:

我已经检查了打开相同对话框的其他应用程序。可能是 PayPal 应用正在监听邮件事件。

【问题讨论】:

我的paypal也有同样的问题 【参考方案1】:

请看这个:

 private void openEmailApp() 
      List<Intent> emailAppLauncherIntents = new ArrayList<>();

      //Intent that only email apps can handle:
      Intent emailAppIntent = new Intent(Intent.ACTION_SENDTO);
      emailAppIntent.setData(Uri.parse("mailto:"));
      emailAppIntent.putExtra(Intent.EXTRA_EMAIL, "");
      emailAppIntent.putExtra(Intent.EXTRA_SUBJECT, "");

      PackageManager packageManager = getPackageManager();

      //All installed apps that can handle email intent:
      List<ResolveInfo> emailApps = packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_ALL);

      for (ResolveInfo resolveInfo : emailApps) 
        String packageName = resolveInfo.activityInfo.packageName;
        Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
        emailAppLauncherIntents.add(launchIntent);
      

      //Create chooser
      Intent chooserIntent = Intent.createChooser(new Intent(), "Select email app:");
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toArray(new Parcelable[emailAppLauncherIntents.size()]));
      startActivity(chooserIntent);
    

已编辑

private void sendEmail(Connect connect) 
    Intent email = new Intent(Intent.ACTION_SENDTO);
    email.setData(Uri.parse("mailto:"));
    email.putExtra(Intent.EXTRA_EMAIL, new String[]connect.getEmail());
    email.putExtra(Intent.EXTRA_SUBJECT, "");
    email.putExtra(Intent.EXTRA_TEXT, "");
    try 
        startActivity(Intent.createChooser(email, getString(R.string.choose_email_client)));
     catch (ActivityNotFoundException activityNotFoundException) 
        UIUtils.showShortSnackBar(fragmentConnectLayout, getString(R.string.no_email_client));
    

【讨论】:

我使用的相同代码,也插入到上述问题中。【参考方案2】:

您可以遍历意图包名称并删除 PayPal。然后使用过滤后的列表创建一个选择器。

val emailAppLauncherIntents = ArrayList<Intent>()

// Intent that only email apps can handle:
val emailAppIntent = Intent(Intent.ACTION_SENDTO)
emailAppIntent.data = Uri.parse("mailto:")

val packageManager = requireActivity().packageManager

// All installed apps that can handle email intent
val emailApps = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
    packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_ALL)
 else 
    packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_DEFAULT_ONLY)


emailApps.forEach  resolveInfo ->
    val packageName = resolveInfo.activityInfo.packageName
    val launchIntent = packageManager.getLaunchIntentForPackage(packageName)

    // Paypal shows up for send intent - we don't want this
    if (!packageName.contains("paypal") && launchIntent != null) 
          emailAppLauncherIntents.add(launchIntent)
    


val chooser = Intent.createChooser(Intent(), getString(R.string.select_email_client))
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toTypedArray())
startActivity(chooser)

【讨论】:

以上是关于Android - 如何从电子邮件选择器列表中排除 PayPal?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 jQuery 选择器中排除 $(this)?

jQuery - 从选择器中排除具有特定值的输入

从联系人选择器意图中选择电子邮件、姓名和电话号码

Android 联系人选择器:选择电话或电子邮件

从 CSS 选择器中排除 div

Android:如何将微调器绑定到自定义对象列表?