浪費後超過3天發現做我想要的方式:
有用於存儲所有類型的數據,即電話,地址,電子郵件等的是一個common structure See the common columns names,
這就是爲什麼我們無法獲得所有類型的數據(聯繫人,電子郵件,地址 等)與接觸選擇器的意圖,所以首先我們需要聯繫 ID或聯繫lookUpKey作爲結果的聯繫人選擇器的意圖,比我們 需要單獨查詢其他細節。
但嘗試獲取其他詳細信息時會出現問題,因爲每個 數據都保存在相同的表結構中,即可以使用 保存任何類型的聯繫人數據的通用表。
並且存儲在給定行中的數據的類型由行的 MIMETYPE值指定,該值確定了通用列 DATA1到DATA15的含義。例如,如果數據Mime類型爲 Phone.CONTENT_ITEM_TYPE,則DATA1列存儲電話號碼 ,但如果數據Mime類型爲Email.CONTENT_ITEM_TYPE,則 DATA1存儲電子郵件地址。
所以首先我們需要查詢 的任何類型的內容之前設置好MIME類型條款,請參閱MIME類型的列表here
這是我們應該如何火的意圖來選擇聯繫人:
int REQUEST_ID_CONTACT_PICKER = 1001;
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, REQUEST_ID_CONTACT_PICKER);
比onActivityResult,我們得到的URI數據:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Contact contact = null;
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_ID_CONTACT_PICKER :
try {
Uri contactURI = intent.getData();
contact = fetchAndBuildContact(getApplicationContext(), contactURI);
Log.d(LOG_TAG, "Pick contact sucessfully!!"+contact.toString());
}
catch (Exception e) {
Log.e(LOG_TAG, "Failed to pick contact!!");
e.printStackTrace();
}
break;
}
} else {
Log.e(LOG_TAG, "Failed to pick contact!!");
}
}
不是我們需要找到LookUpKey,有利於獲取任何MIME類型的數據:
private Contact fetchAndBuildContact(Context ctx, Uri uriContact){
// Getting cursorLookUpKey because contacts ID may not be correct all the time.
Cursor cursorLookUpKey = ctx.getContentResolver().query(uriContact, new String[]{ContactsContract.Data.LOOKUP_KEY}, null, null, null);
Contact contact = null;
String lookupKey = null;
if (cursorLookUpKey.moveToFirst()) {
lookupKey = cursorLookUpKey.getString(cursorLookUpKey.getColumnIndex(ContactsContract.Data.LOOKUP_KEY));
if(null != lookupKey){
contact = new Contact();
contact = buildConactPhoneDetails(lookupKey, ctx, contact);
contact = buildEmailDetails(lookupKey, ctx, contact);
contact = buildAddressDetails(lookupKey, ctx, contact);
}
}
cursorLookUpKey.close();
return contact;
}
這就是我們如何能夠利用ContactsContract.Data.MIMETYPE = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
獲取手機詳情:
private Contact buildConactPhoneDetails(String lookupKey, Context ctx, final Contact contact) {
ContentResolver contentResolver = ctx.getContentResolver();
String contactWhere = ContactsContract.Data.LOOKUP_KEY + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] contactWhereParams = new String[]{lookupKey, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
Cursor cursorPhone = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, contactWhere, contactWhereParams, null);
if (cursorPhone.getCount() > 0) {
if (cursorPhone.moveToNext()) {
if (Integer.parseInt(cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
String givenName = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String familyName = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String middleName = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
int contactType = cursorPhone.getInt(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNo = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.contactNumber = phoneNo;
contact.givenName = givenName;
contact.familyName = familyName;
contact.middleName = middleName;
contact.contactType = contactType;
}
}
}
cursorPhone.close();
return contact;
}
這是我們如何可以使用ContactsContract.Data.MIMETYPE = ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE
獲取電子郵件詳細信息:
private Contact buildEmailDetails (String lookupKey, Context ctx, final Contact contact) {
ContentResolver contentResolver = ctx.getContentResolver();
String emailWhere = ContactsContract.Data.LOOKUP_KEY+ " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] emailWhereParams = new String[]{lookupKey, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
Cursor emailCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, emailWhere, emailWhereParams, null);
if (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
contact.emailId = emailId;
}
emailCursor.close();
return contact;
}
這就是我們如何能夠利用ContactsContract.Data.MIMETYPE = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
獲取PhoneDetails:
private Contact buildAddressDetails(String lookupKey, Context ctx, final Contact contact) {
ContentResolver contentResolver = ctx.getContentResolver();
String addrWhere = ContactsContract.Data.LOOKUP_KEY + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[]{lookupKey, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);
if (addrCursor.moveToNext()) {
String poBox = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String neighborhood = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD));
String formattedAddress = addrCursor.getString(addrCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
contact.poBox = poBox;
contact.street = street;
contact.city = city;
contact.state = state;
contact.zipcode = postalCode;
contact.country = country;
contact.street = street;
contact.neighborhood = neighborhood;
contact.poBox = poBox;
contact.formattedAddress = formattedAddress;
}
addrCursor.close();
return contact;
}
,這是聯繫POJO類,我們在上面的代碼中創建:
public class Contact {
String country;
String poBox;
String givenName;
String middleName;
String familyName;
String address;
String city;
String state;
String street;
String zipcode;
String emailId;
String contactNumber;
String neighborhood;
String formattedAddress;
String label;
int contactType;
}
這是一個很好的一段代碼,謝謝你的分享!值得更多upvotes。 – azizbekian