Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extras on Email Intent - Preferences XML

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

Here's what I've got so far

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

    <intent
      android:action="android.intent.action.VIEW"
      android:data="mailto:[email protected]"
      />

  </Preference>

Works great for triggering the email intent, but how would i go about accomplishing the others via xml? attaching the subject and all?

like image 276
jondavidjohn Avatar asked Aug 04 '11 21:08

jondavidjohn


People also ask

How do I set extras intent?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

What is Android intent action view?

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.

Where would you use intents?

Intents are used to signal to the Android system that a certain event has occurred. Intents often describe the action which should be performed and provide data upon which such an action should be done. For example, your application can start a browser component for a certain URL via an intent.


1 Answers

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:[email protected]?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:[email protected]?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.

like image 137
Matt Gibson Avatar answered Sep 27 '22 23:09

Matt Gibson