ContactsContract 如何枚举所有可用字段?
Posted
技术标签:
【中文标题】ContactsContract 如何枚举所有可用字段?【英文标题】:ContactsContract how to enumerate all available fields? 【发布时间】:2012-05-19 08:27:27 【问题描述】:在我的应用程序中,我需要让用户有机会查看/编辑 ContactsContract 的所有可用字段。
我应该如何获取/查看所有可用字段 - 包括任何自定义字段?
【问题讨论】:
【参考方案1】:首先获取您手机中存在的所有人员的姓名:
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
while (phones.moveToNext())
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
namelist.add(name);
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
number_list.add(phoneNumber);
detaillist.put(name, phoneNumber);
phones.close();
现在获取联系人 ID 并使用联系人 ID 获取其他信息,例如电话号码、电子邮件 ID、地址、组织:
protected void get_UserContactId(String item_clicked)
System.out.println("i m in fincution contact id");
ContentResolver localContentResolver = this.getContentResolver();
Cursor contactLookupCursor =
localContentResolver.query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(item_clicked)),
new String[] PhoneLookup.DISPLAY_NAME, PhoneLookup._ID,
null,
null,
null);
try
while(contactLookupCursor.moveToNext())
contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
user_contact_id = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
System.out.println("contatc id id"+user_contact_id);
finally
contactLookupCursor.close();
protected void get_UserEmail(String item_clicked)
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + user_contact_id, null, null);
while (emails.moveToNext())
// This would allow you get several email addresses
user_email_id = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String user_email_type=emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
System.out.println("the email type"+user_email_type);
System.out.println("email address might be"+emailAddress);
contact_attribute_type.add(EMAIL_TYPE[(Integer.parseInt(user_email_type))-1]);
contact_attribute_value.add(user_email_id);
emails.close();
protected void get_UserNumber(String item_clicked)
// TODO Auto-generated method stub
final String[] CONTACTS_SUMMARY_PROJECTION = new String[]
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.TIMES_CONTACTED,
ContactsContract.Contacts.CONTACT_PRESENCE,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
;
String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +item_clicked+ "\" )";
Cursor c = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
this.startManagingCursor(c);
if (c.moveToNext())
String id = c.getString(0);
ArrayList<String> phones = new ArrayList<String>();
Cursor pCur = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]id, null);
while (pCur.moveToNext())
phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
user_number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String number_type=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
System.out.println("the number type---"+number_type);
System.out.println("user no. in share is"+user_number);
contact_attribute_type.add(PHONE_TYPE[(Integer.parseInt(number_type))-1]);
contact_attribute_value.add(user_number);
// Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
pCur.close();
protected String get_UserAddress()
// TODO Auto-generated method stub
try
System.out.println(" i m in user address");
Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + client_contact_id, null, null);
while (address.moveToNext())
// This would allow you get several email addresses
user_home_address = address.getString(
address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));
String user_address_type=address.getString(
address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
System.out.println("the user address type"+user_address_type);
System.out.println(" address might be"+user_home_address);
contact_attribute_type.add(ADDRESS_TYPE[(Integer.parseInt(user_address_type))-1]);
contact_attribute_value.add(user_home_address);
address.close();
catch(Exception e)
System.out.println("this exception due to website"+e);
return(user_home_address);
protected void get_UserWebsite()
// TODO Auto-generated method stub
try
Cursor websiteNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] Website.URL, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
+ ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE
+ "'",null,null);
websiteNameCursor.moveToNext();
user_website=(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL)));
System.out.println("this is my website"+(websiteNameCursor.getString(websiteNameCursor.getColumnIndex(Website.URL))));
contact_attribute_type.add("Website");
contact_attribute_value.add(user_website);
catch(Exception e)
user_website=null;
System.out.println("this website is"+user_website);
System.out.println("this exception in website"+e);
protected void get_UserOrganization()
// TODO Auto-generated method stub
try
Cursor organizationNameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,new String[] Organization.COMPANY, ContactsContract.Data.CONTACT_ID + " = " + user_contact_id + " AND ContactsContract.Data.MIMETYPE = '"
+ ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
+ "'",null,null);
organizationNameCursor.moveToNext();
user_company=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.COMPANY));
// String user_company_type=organizationNameCursor.getString(organizationNameCursor.getColumnIndex(Organization.TYPE));
// System.out.println("the company type "+user_company_type);
System.out.println("this is my organization"+user_company);
contact_attribute_type.add("Company");
contact_attribute_value.add(user_company);
catch(Exception e)
user_company=null;
System.out.println("user company name is"+user_company);
System.out.println("this exception in org"+e);
这样你就可以获得contacts-contract的所有字段。
【讨论】:
Thanx,但是如果那里有一些用户定义的字段怎么办?你能启发我吗? 嘿,barmaley,这个contactscontract类让你可以完全访问用户定义的所有字段,如果你想访问更多填充然后首先点击这个链接developer.android.com/reference/android/provider/…这只是概念明智的url现在我给一个很好的网址,它可以帮助你实施higherpass.com/Android/Tutorials/Working-With-Android-Contacts,如果它对你有用,请给我投票并喜欢这个评论。【参考方案2】:这会记录所有联系人行中所有列的名称和值。使用原生联系人应用在 Galaxy S6 上进行测试。我需要它来查找用于自定义 SMS 通知声音 (sec_custom_alert) 的列名。
private void enumerateColumns()
Cursor cursor = getApplicationContext().getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor != null && cursor.moveToFirst())
do
for(int i=0; i<cursor.getColumnCount();i++)
Log.d("myActivity", cursor.getColumnName(i) + " : " + cursor.getString(i));
while (cursor.moveToNext());
cursor.close();
【讨论】:
以上是关于ContactsContract 如何枚举所有可用字段?的主要内容,如果未能解决你的问题,请参考以下文章