如何等待电子邮件意图完成并获得结果?
在我的 Android 应用程序中,我可以使用以下命令以编程方式打开包含“收件人”、“主题”和“消息”的默认电子邮件编辑器:
Intent emailIntent=new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, toemail);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.setType("text/plain");
emailIntent.setClassName("com.android.email", "com.android.email.activity.MessageCompose");
startActivity(emailIntent);
这效果很好,但我需要在我的应用程序中等待,直到用户完成电子邮件屏幕并了解电子邮件是否已发送或丢弃。
有人知道该怎么做吗?
In my Android app, I am able to programmatically open up the default email editor with To, Subject, and Message using the following:
Intent emailIntent=new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, toemail);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
emailIntent.setType("text/plain");
emailIntent.setClassName("com.android.email", "com.android.email.activity.MessageCompose");
startActivity(emailIntent);
This works great, but I need to wait in my app until the user finishes with the email screen and also know whether the email was sent or discarded.
Anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,可以使用 startActivityForResult() 来启动第二个活动作为子活动。但是,在电子邮件活动的情况下,这似乎不起作用,可能是由于内部实施的原因。在发布问题之前尝试搜索:
我们如何使用 startActivityforResult() 进行电子邮件意图?
电子邮件的实际发送在设计上是异步的,因此该活动可能会在电子邮件实际发送之前返回。我没有专门测试这种情况,但从上面的链接来看,一旦用户点击发送按钮,活动就会返回。如果这足以满足您的用例,那么超级,如果您需要知道电子邮件是否确实已发送,您可能会很满意。
Normally, one could use startActivityForResult() which starts the second activity as a sub-activity. However, in the case of the email activity this doesn't appear to work, likely because of the internal implementation. Try searching before posting questions:
how can we use startActivityforResult() for Email intent?
The actual sending of an email is asynchronous by design, so the activity will likely return before the email is actually sent. I haven't tested this case specifically, but from the above link it seems that the activity returns once the user hits the send button. If this suffices for your use case then super, if you need to know if the email was actually sent you might be SOL.
这会很棘手。电子邮件发送没有标准化的返回值,并且根据用户的设置,可以使用电子邮件应用程序、Gmail 应用程序或众多第三方电子邮件应用程序之一发送邮件。他们很可能在处理结束电子邮件的方式上都有所不同。
至于您的一般问题,您可以使用
startActivityForResult()
启动一个活动,然后在完成后继续,并返回一个指示事情进展情况的值。但是,我很确定大多数电子邮件应用程序不会在这里为您提供正确的结果。我很想说,您可能需要自己处理电子邮件的发送,即编写一个简单的函数来连接到 SMTP 并将邮件发送出去。我确信有很多图书馆可以处理所有繁重的工作。
(当然,您可以首先尝试
startActivityForResult
- 也许最常见的电子邮件应用程序确实会给您返回值。)This is going to be tricky. There is no standardized return value for email sending, and depending on the user's settings, the mail could be sent with the Email app, the Gmail app, or one of the many 3rd party email apps. They most likely all differ in how they handle ending the emails.
As for your question in general, you can use
startActivityForResult()
to start an activity and then continue after it finishes, with a return value indicating how things went. However, I'm pretty sure that most email apps won't give you a proper result here.I'm tempted to say that you may need to handle the sending of the email yourself, i.e. write a simple function that connects to the SMTP and sends the mail out. I'm sure there are lots of libraries out there that handle all the heavy-lifting.
(You can, of course, experiment with
startActivityForResult
first - MAYBE the most common email apps do give you a return value.)