Need to pick contact from a dialog preference
I would like to add a preference setting that uses an ACTION_PICK
intent. My goal is to acquire the phone number of a contact in my phone by using a preference. Is this possible?
I can run this code from my activity but I discovered I cannot run it from a class that extends DialogPreference
.
Intent intentContact = new Intent(Int开发者_如何学编程ent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intentContact, PICK_CONTACT);
Or is there a way to start a new Activity
from a Preference
? Then that Activity
could execute the above two lines of code?
// Get Custom contact Pref
Preference customContact = (Preference)findPreference("custom_contact");
customContact.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
return true;
}
});
You have to define the contact preference as "Preference ..." in preferences.xml, if you use "EditTextPreference..." the text edit dialog is shown, and this is not convenient in this case.
精彩评论