8

我需要检查设备上是否安装了电子邮件客户端。我使用了以下代码,但它对我不起作用。

public boolean isIntentAvailable() {
    final PackageManager packageManager = getApplicationContext().getPackageManager();
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, "vin@gmail.com");
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.GET_META_DATA);
    return list.size() > 0;
} 
4

3 回答 3

13

使用这个,对我有用:

public static boolean isMailClientPresent(Context context){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/html");
    final PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);

    if(list.size() == 0)
        return false;
    else 
        return true;
}
于 2012-09-27T08:34:39.523 回答
3

对于电子邮件客户端,具体来说,您应该使用:

intent.setType("message/rfc822");

代替:

intent.setType("text/html");
于 2015-05-07T11:25:19.157 回答
3

使用这个方法:

    private fun sendEmail(to: Array<String>) {
        val intent = Intent(Intent.ACTION_SENDTO)
        intent.data = Uri.parse("mailto:") // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, to)
//        intent.putExtra(Intent.EXTRA_SUBJECT, subject)
        if (intent.resolveActivity(requireContext().packageManager) != null) {
            startActivity(intent)
        }
    }

为了能够在针对 api 30 时检查电子邮件客户端,请将“查询”添加到清单中:

<queries>
    <intent>
        <action android:name="android.intent.action.SENDTO" />
        <data android:scheme="*" />
    </intent>
</queries>
于 2020-12-06T08:11:52.993 回答