gpt4 book ai didi

android - 为什么这个 Android Intent 找不到电子邮件应用程序?

转载 作者:行者123 更新时间:2023-11-29 17:31:22 25 4
gpt4 key购买 nike

我已经设置了一个电子邮件 Intent 并尝试启动它。代码如下:

    public void emailSummary(String emailText, String name) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, "");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "JustJava order for " + name);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
Log.v("MainActivity", "Finished setting up intent");
if (emailIntent.resolveActivity(getPackageManager()) != null) {
startActivity(emailIntent);
Log.v("MainActivity", "Sharing the order summary to email");
} else {
Log.d("MainActivity", "No email app installed!");
Toast.makeText(MainActivity.this, "Uh...No email app?", Toast.LENGTH_SHORT).show();
}

但是,当我按下订单按钮时​​,“呃……没有电子邮件应用程序?” toast 出现了。我同时安装了 Inbox 和 Gmail,但是 Intent 似乎没有找到它们。为什么?

最佳答案

您可以使用此代码:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto",name, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "SOME_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
try {
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Log.v("MainActivity", "Sharing the order summary to email");
}
catch (android.content.ActivityNotFoundException ex) {
Log.d("MainActivity", "No email app installed!");
Toast.makeText(MainActivity.this, "Uh...No email app?", Toast.LENGTH_SHORT).show();
}

can you please explain what was wrong with my code?

我已经测试了您的代码,if (emailIntent.resolveActivity(getPackageManager()) != null) { 无法检查您的设备上是否有电子邮件 APP。我找到了这个方法并且工作正常:@Nitin answer

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;
}

关于android - 为什么这个 Android Intent 找不到电子邮件应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32806787/

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