kotlin 我想通过电子邮件发送消息,但Intent不起作用,为什么?

e0bqpujr  于 2022-11-25  发布在  Kotlin
关注(0)|答案(3)|浏览(130)

我的应用程序中有一个部分,我使用按钮发送电子邮件,但由于某种原因,意图不起作用,我不明白为什么。

binding.IvMail.setOnClickListener {
    val email = Intent(Intent.ACTION_SEND)
        .setType("text/plain")
        .putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
        .putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))

    if (activity?.packageManager?.resolveActivity(email, 0) != null) {
        startActivity(email)
    }

}

我已经搜索了其他方法来做这件事,但每个人都在使用意图。

jei2mxaa

jei2mxaa1#

// try this(JAVA).
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
    ////For(Kotlin)
        sendEmail(recipient, subject, message)

 private fun sendEmail(recipient: String, subject: String, message: 
String) {
        /*ACTION_SEND action to launch an email client installed on your Android device.*/
        val mIntent = Intent(Intent.ACTION_SEND)
        /*To send an email you need to specify mailto: as URI using 
setData() method
        and data type will be to text/plain using setType() method*/
        mIntent.data = Uri.parse("mailto:")
        mIntent.type = "text/plain"
        // put recipient email in intent
        /* recipient is put as array because you may wanna send email to multiple emails
           so enter comma(,) separated emails, it will be stored array*/
        mIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
        //put the Subject in the intent
        mIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
        //put the message in the intent
        mIntent.putExtra(Intent.EXTRA_TEXT, message)

        try {
            //start email intent
            startActivity(Intent.createChooser(mIntent, "Choose Email Client..."))
        }
        catch (e: Exception){
            //if any thing goes wrong for example no email client application or any exception
            //get and show exception message
            Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
        }

    }
}
xqnpmsa8

xqnpmsa82#

您必须使用ACTION_SENDTO而不是ACTION_SEND
请参阅下面的新代码:

binding.IvMail.setOnClickListener {
    val email = Intent(Intent.ACTION_SENDTO) // here you have to use SENDTO
        .setType("text/plain")
        .putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
        .putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))

    if (activity?.packageManager?.resolveActivity(email, 0) != null) {
        startActivity(email)
    }

}

如果尚未解决,请通知我

sc4hvdpw

sc4hvdpw3#

您的代码没有问题。如果它没有运行,则可能是您的if语句未得到满足,正在跳过Intent解析。
您可以执行以下操作来确定问题:
1.通过设置断点并运行如下调试应用程序,在到达代码块时进行调试:

1.通过设置日志并在logcat工具中查找它们,记录是否达到上述代码块,如下所示:

1.您还可以在Logcat中查找运行Intent时发生的错误。

相关问题