为什么要使用Intent.createChooser()? [英] Why use Intent.createChooser()?
问题描述
我发现了一些code发送电子邮件;它看起来是这样的:
I found some code to send an email; it looks like this:
startActivity(Intent.createChooser(emailIntent, "Email"));
我改成了:
startActivity(emailIntent);
和结果出现在我的设备上是相同的。为什么要使用 Intent.createChooser()
And the result appears the same on my device. Why use Intent.createChooser()
?
推荐答案
什么你基本上要求系统在第二个语句中去发现,可向意图android.content.Intent.ACTION_SEND,并与响应活动在声明中键入set
What you basically ask the system in the second statement is go find the Activity that can respond to the Intent "android.content.Intent.ACTION_SEND" and with the type set in the statement
emailIntent.setType("message/rfc822"); // or "text/plain"
但在第一条语句您所请求的Android应用程序选择器,如果有零/一个/或一个以上的活动来处理这个意图要显示不管。
but in the first statement you request the Android Application chooser to be displayed no matter if there is zero/one/or more than one Activity to handle the Intent.
另外,如果你使用的语句
Also, if you use the statement
startActivity(emailIntent);
和没有电子邮件客户端对你的意图(例如,在仿真器),然后作出反应的应用程序与扔ActivityNotFoundException,但如果你使用:
And there is no email clients to respond to your intent (for example in the Emulator) then the App with throw "ActivityNotFoundException",but if you use:
startActivity(Intent.createChooser(emailIntent, "Email"));
和未安装电子邮件客户端,然后应用程序会显示错误消息无应用程序可以执行此操作。
And no email client is installed, then the App will display the error message "No Application can perform this action".
这篇关于为什么要使用Intent.createChooser()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!