Extras on Email Intent - Preferences XML

前端 未结 2 810
无人共我
无人共我 2021-01-04 23:57

I\'m wanting to trigger an Email from my xml preferences screen and also attach a pre-defined subject and start the cursor in the Body field of the email application

相关标签:
2条回答
  • 2021-01-05 00:54

    Apparently you can use many of the querystring arguments that you can on the normal browser mailto: uri.

    So to accomplish this you just have to use them like this.

    <intent
      android:action="android.intent.action.VIEW"
      android:data="mailto:xxxxx@xxxxxxx.com?subject=this is a test subject"
      />
    
    0 讨论(0)
  • 2021-01-05 01:01

    You can use both mailto query parameters, as jondavidjohn says, and also intent extras, and you can mix and match them both. For example:

    <intent
      android:action="android.intent.action.VIEW"
      android:data="mailto:xxxxx@xxxxxxx.com?subject=this is a test subject">
      <extra android:name="android.intent.extra.TEXT" android:value="This is a test" />
    </intent>
    

    ...will allow you to set the body of an email as well as the subject. You can also specify the subject as an extra. This lets you use XML string resources rather than hardcoding, too:

      <extra android:name="android.intent.extra.SUBJECT" android:value="@string/email_subject" />
    

    I just grabbed the Intent extra names from Intent.java; the email-related ones are all in a bunch together.

    I've only just discovered this and haven't done much testing, but this certainly seems to work with my GMail mail client.

    Also, if it's any help, I did have success using the "body" of the mailto: URI, e.g.

     mailto:example@example.com?subject=This%20is%20a%20subject&body=This%20is%20a%20body
    

    Don't know whether it helped that I url-encoded my mailto URL; I was just doing it through force of habit, coming from a web background. But that definitely works and sets a body in both GMail and K9 Mail apps.

    0 讨论(0)
提交回复
热议问题