2

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

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 应用正在收听邮件事件。

4

2 回答 2

0

您可以遍历意图包名称并删除 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)
于 2020-11-25T19:39:49.657 回答
0

请调查一下:

 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));
    }
}
于 2020-06-08T06:34:44.777 回答