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) findPre开发者_StackOverflowference("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[]{"test@gmail.com"});
mailto.putExtra(Intent.EXTRA_SUBJECT,"Subject Here");
mailto.putExtra(Intent.EXTRA_TEXT,"Body Here");
startActivity(Intent.createChooser(mailto, "Select email application."));
return true;
}
});
}
}
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:my@email.address</string>
The limitation is no predefined subject/body which is possible using the Java method about along with extras. Adding extra
s 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 value
s.
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=
.
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. Refer to http://snipplr.com/view/19973/send-email-from-android-using-intent/
精彩评论