Android:使用 Intent.ACTION_SEND / requestCode / resultCode 发送邮件/短信/推文?
我正在使用以下代码:
Intent sendMailIntent = new Intent(Intent.ACTION_SEND);
sendMailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.Share_Mail_Subject));
sendMailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.Share_Mail_Text));
sendMailIntent.setType("text/plain");
startActivity(Intent.createChooser(sendMailIntent, "Email / SMS / Tweet ?"));
然后我希望能够区分: 1.我的用户确实发送了电子邮件/短信...或者 2.我的用户实际上已经按下了“后退”按钮...并且没有发送任何内容。
有没有办法改变这种差异?
=>我应该使用 startActivityForResult 启动活动吗?并使用 onActivityResult 捕获 requestCode/resultCode ...
=>我应该期待什么结果代码之王?如何正确抓取?我应该把这些代码行放在哪里?任何代码片段在这里都会非常有帮助。
提前致谢。
中心
I'm using the following code :
Intent sendMailIntent = new Intent(Intent.ACTION_SEND);
sendMailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.Share_Mail_Subject));
sendMailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.Share_Mail_Text));
sendMailIntent.setType("text/plain");
startActivity(Intent.createChooser(sendMailIntent, "Email / SMS / Tweet ?"));
Then I would like to be able to make the difference between:
1. my user has indeed sent en email/SMS ... OR
2. my user has in fact pushed the BACK BUTTON ... and didn't send anything.
Is there a way to make this difference ?
=> Should I launch the activity with startActivityForResult ? and catch the requestCode/resultCode with onActivityResult ...
=> What king of resultCode should I expect ? how to grab it correctly ? Where should I put these lines of code ? Any snippet of code would be very helpful here.
thanks in advance.
Hub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我意识到自从你问这个问题以来已经有一段时间了,Android 在这段时间里发生了很大的变化。我不确定您是否仍在寻找答案,但如果是,您可以使用新的
Intent.createChooser()
方法来完成此操作,该方法需要第三个PendingIntent.getIntentSender ()
参数和一个BroadcastReceiver
。具体操作方法如下:请注意,我的
receiver
意图的目标是扩展BroadcastReceiver
的BroadcastTest
类。当用户从选择器中选择一个应用程序时,BroadcastTest
中的onReceive
方法将被调用,如果用户按回键,onReceive
将不会被调用。叫。这样,您可以检查用户是否确实发送了电子邮件/短信/推文,或者他们是否按下了回复。例如,如果这是我的BroadcastTest
类:如果用户选择了应用程序 Telegram。使用
android.intent.extra.CHOSEN_COMPONENT
键,您应该能够找到用户选择的内容。另外,不要忘记在清单中声明BroadcastReceiver
。另一种方法是使用
PackageManager
和queryIntentActivities()
来制作您自己的选择器。这将允许您以编程方式获取用户选择。该方法在此中进行了描述StackOverflow 帖子。为了解决您关于
startActivityForResult
的问题,从 Android 的源代码中,您可以看到在Intents
中选择的Activity
不会setResult( )
根本就没有。因此,如果您尝试在onActivityResult
中捕获结果代码,它将始终为 0 (RESULT_CANCELED
)。因此,使用startActivityForResult
,您无法确定用户是否选择了某个选项或按下了返回键。I realize it has been quite a while since you asked this question and Android has changed quite a bit during this time. I'm not sure if you are still looking for an answer, but if you are, you can do this with the new
Intent.createChooser()
method, which takes a thirdPendingIntent.getIntentSender()
argument, and aBroadcastReceiver
. Here's how you do it:Note that the target for my
receiver
intent was theBroadcastTest
class which extendsBroadcastReceiver
. When the user chooses an application from the chooser, theonReceive
method inBroadcastTest
will be called and if the user presses back,onReceive
will not be called. This way, you can check whether the user indeed sent an email/sms/tweet or if they pressed back. For example, if this is myBroadcastTest
class:you would get something like
ComponentInfo{org.telegram.messenger/org.telegram.ui.LaunchActivity}
in your log if the user selected the application Telegram. Using the keyandroid.intent.extra.CHOSEN_COMPONENT
, you should be able to find what the user picked. Also, don't forget to declare theBroadcastReceiver
in your manifest.Another way is to use
PackageManager
andqueryIntentActivities()
to make your own chooser. This would allow you to programmatically get the user selection. The method is described in this StackOverflow post.To address your question about
startActivityForResult
, from Android's source, you can see that theActivity
that chooses amongIntents
doesn'tsetResult()
at all. Thus, if you try to catch a result code inonActivityResult
, it will always be 0 (RESULT_CANCELED
). Thus, usingstartActivityForResult
, you cannot determine whether the user selected an option or pressed back.