Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailto from preferences xml possible?

I am trying to build out the preferences for my application and I was hoping to do a "Contact the developer" area where when clicked, it would open an email directed to me. Is this possible to do from the xml file alone or do I need to do stuff in the main class?

I searched here a bit but did not see anything about doing it from XML so maybe thats not possible? Thought I would throw this question out there.

Thanks!

EDIT: This is how I actually got it to work for anyone in the future looking for some code:

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

public class Prefs extends PreferenceActivity {

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.layout.prefs);
    Preference mailTo = (Preference) findPreference("mailTo");


    mailTo.setOnPreferenceClickListener(new OnPreferenceClickListener() 
  {
   public boolean onPreferenceClick(Preference preference) 
   {
        // Preferences

        Intent mailto = new Intent(Intent.ACTION_SEND); 
        mailto.setType("message/rfc822") ; // use from live device
        mailto.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
        mailto.putExtra(Intent.EXTRA_SUBJECT,"Subject Here");
        mailto.putExtra(Intent.EXTRA_TEXT,"Body Here");
        startActivity(Intent.createChooser(mailto, "Select email application."));
    return true;
   }
  });

}

}

like image 627
Rob Avatar asked Jun 12 '11 00:06

Rob


People also ask

How do I use a mailto?

The following code shows how to use a mailto: uniform resource identifier (URI) to open a new mail window that contains an email address, and email address and a subject, and an email address, subject, and body. Represents a control that allows the user to select a date. Manually closes a Window. Implements a set of predefined colors.

Is it possible to send an email from an XML file?

It's not possible from xml. However it's not a lot of work because of the way android works. The only thing that needs to be done is sending an intent that notifies the system you want to send an email, with the details you provide. Apps that are able to do this will respond to this intent and handle the rest for you.

Where can I find more information on the mailto protocol?

For more information on the mailto protocol, see RFC2368: The mailto URL scheme. The following example shows a mailto URL that will prepare an e-mail message when typed into the Internet Explorer address bar. The following example shows a link that will prepare an e-mail message.

How to set Chrome as the default program to open mailto?

You need the Chrome template (downloadable) and you import into Group Policy. to set chrome as the default program, to open MAILTO: and ENABLE the policy. So all this is in place LOOKS GREAT! BUT in my environment I found out that Chrome is set to a "RECOMMENDED" method to open thus whatever the OS see's will take presidence.


1 Answers

You can do it directly from preferences:

<Preference
        android:key="contactDevKey"
        android:title="Contact developer"
        android:summary="Tell me all about your problems">
    <intent android:action="android.intent.action.VIEW"
            android:data="@string/contact_developer_uri"/>
</Preference>

Where @string/contact_developer_uri is:

<string name="contact_developer_uri">mailto:[email protected]</string>

The limitation is no predefined subject/body which is possible using the Java method about along with extras. Adding extras to <intent>s are supported since 4.0 Ice Cream Sandwich thanks to this commit (see commit tags). It's a side effect of allowing extras for fragments. So you can provide a template as Andrew suggested in the comments:

<intent android:action="android.intent.action.VIEW"
        android:data="@string/contact_developer_uri">
    <extra android:name="android.intent.extra.TEXT"
           android:value="What can I help you with?" />
    <extra android:name="android.intent.extra.SUBJECT"
           android:value="Feedback about World Changing App" />
</intent>

Using resource references are encouraged, but not necessary for both data and values.

Sadly you can't use Intent.ACTION_SEND this way, because EXTRA_EMAIL needs to be a String[] and that's not supported as <extra android:value=.

like image 68
roflharrison Avatar answered Oct 04 '22 02:10

roflharrison