0

In Android 12 if you add informations as subject(EXTRA_SUBJECT) and message(EXTRA_TEXT) when you use ACTION_SENDTO Intent to send a text email, these don't appear in the email client message, contrarely to all previous versions.

An user in a similar Kotlin question seems has solved the problem using apply selector in this way:

private fun createIntent(
    metadata: String
): Intent {
    return Intent(ACTION_SEND)
        .putExtra(
            EXTRA_EMAIL,
            arrayOf(EMAIL)
        )
        .putExtra(
            EXTRA_SUBJECT,
            TITLE
        )
        .putExtra(
            EXTRA_TEXT,
            metadata
        )
        .apply {
            selector = Intent(ACTION_SENDTO).setData(Uri.parse("mailto:"))
        }
}

What is the reason of this issue? What is the proper way to fix the issue in Java?

AndreaF
  • 11,975
  • 27
  • 102
  • 168

1 Answers1

0

these don't appear in the email client message, contrarely to all previous versions

How apps handle inbound extras is up to the developers of the apps. And, since ACTION_SENDTO is not documented to have those extras, you should not be surprised when apps ignore those extras.

What is the proper way to fix the issue in Java?

Do what you have there in Kotlin, if ACTION_SEND will work for you. Intent works the same way whether you use it in Java, Kotlin, or any other suitable programming language. Your selector will limit your Intent to apps that have activities that support that mailto: Uri for ACTION_SENDTO (which may be more than just email apps).

By eyeball, the Java equivalent should be something like this:

private Intent createIntent(String metadata) {
    Intent result = new Intent(ACTION_SEND)
        .putExtra(
            EXTRA_EMAIL,
            new String[] { EMAIL }
        )
        .putExtra(
            EXTRA_SUBJECT,
            TITLE
        )
        .putExtra(
            EXTRA_TEXT,
            metadata
        );
        
    result.setSelector(new Intent(ACTION_SENDTO).setData(Uri.parse("mailto:")));
       
    return result;
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491