电子邮件意图的附加内容 - 首选项XML

jon*_*ohn 9 xml email android preferences android-intent

我想从我的xml偏好设置屏幕触发电子邮件,并附加预定义的主题并在电子邮件应用程序的正文字段中启动光标

这是我到目前为止所得到的

  <Preference
    android:title="Support"
    android:summary="Having a problem?">

    <intent
      android:action="android.intent.action.VIEW"
      android:data="mailto:support@xxxxx.com"
      />

  </Preference>
Run Code Online (Sandbox Code Playgroud)

非常适合触发电子邮件意图,但我如何通过xml完成​​其他工作呢?附加主题和所有?

Mat*_*son 21

您可以像jondavidjohn所说的那样使用mailto查询参数,也可以使用intent extras,并且可以将它们混合使用.例如:

<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>
Run Code Online (Sandbox Code Playgroud)

...将允许您设置电子邮件的正文以及主题.您还可以将主题指定为额外的.这使您可以使用XML字符串资源而不是硬编码:

  <extra android:name="android.intent.extra.SUBJECT" android:value="@string/email_subject" />
Run Code Online (Sandbox Code Playgroud)

我只是从Intent.java中获取了Intent额外的名字; 与电子邮件相关的所有内容都在一起.

我刚刚发现了这个,并没有做太多的测试,但这似乎与我的GMail邮件客户端一起工作.

此外,如果有任何帮助,我确实使用mailto:URI的"正文"成功,例如

 mailto:example@example.com?subject=This%20is%20a%20subject&body=This%20is%20a%20body
Run Code Online (Sandbox Code Playgroud)

不知道我是否对我的mailto URL进行了url编码是否有帮助; 我只是通过习惯来做这件事,来自网络背景.但这肯定有效,并在GMail和K9 Mail应用程序中设置了一个机构.