Android:使用 Intent.ACTION_SEND / requestCode / resultCode 发送邮件/短信/推文?

发布于 2024-08-07 17:22:48 字数 701 浏览 6 评论 0原文

我正在使用以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2024-08-14 17:22:48

我意识到自从你问这个问题以来已经有一段时间了,Android 在这段时间里发生了很大的变化。我不确定您是否仍在寻找答案,但如果是,您可以使用新的 Intent.createChooser() 方法来完成此操作,该方法需要第三个 PendingIntent.getIntentSender () 参数和一个 BroadcastReceiver。具体操作方法如下:

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");

Intent receiver = new Intent(this, BroadcastTest.class);
receiver.putExtra("test", "test");

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());
startActivity(chooser);

请注意,我的 receiver 意图的目标是扩展 BroadcastReceiverBroadcastTest 类。当用户从选择器中选择一个应用程序时,BroadcastTest 中的 onReceive 方法将被调用,如果用户按回键,onReceive 将不会被调用。叫。这样,您可以检查用户是否确实发送了电子邮件/短信/推文,或者他们是否按下了回复。例如,如果这是我的 BroadcastTest 类:

public class BroadcastTest extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        for (String key : intent.getExtras().keySet()) {
            Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
        }
    }
}

如果用户选择了应用程序 Telegram。使用android.intent.extra.CHOSEN_COMPONENT键,您应该能够找到用户选择的内容。另外,不要忘记在清单中声明 BroadcastReceiver

另一种方法是使用 PackageManagerqueryIntentActivities() 来制作您自己的选择器。这将允许您以编程方式获取用户选择。该方法在中进行了描述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 third PendingIntent.getIntentSender() argument, and a BroadcastReceiver. Here's how you do it:

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");

Intent receiver = new Intent(this, BroadcastTest.class);
receiver.putExtra("test", "test");

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender());
startActivity(chooser);

Note that the target for my receiver intent was the BroadcastTest class which extends BroadcastReceiver. When the user chooses an application from the chooser, the onReceive method in BroadcastTest 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 my BroadcastTest class:

public class BroadcastTest extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        for (String key : intent.getExtras().keySet()) {
            Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key));
        }
    }
}

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 key android.intent.extra.CHOSEN_COMPONENT, you should be able to find what the user picked. Also, don't forget to declare the BroadcastReceiver in your manifest.

Another way is to use PackageManager and queryIntentActivities() 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 the Activity that chooses among Intents doesn't setResult() at all. Thus, if you try to catch a result code in onActivityResult, it will always be 0 (RESULT_CANCELED). Thus, using startActivityForResult, you cannot determine whether the user selected an option or pressed back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文