如何获取android系统中的联系人和电话号码
android系统的联系人数据库是非常的复杂,并不是一个简单的表就能搞定的。
要简单的查询用户名和电话号码,首先要查询到姓名,然后再进局姓名去查询电话号码,一个姓名可能对应多个电话号码:
// These are the Contacts rows that we will retrieve. static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]{ ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.CONTACT_STATUS, ContactsContract.Contacts.CONTACT_PRESENCE, ContactsContract.Contacts.PHOTO_ID, ContactsContract.Contacts.LOOKUP_KEY, } Uri baseUri = ContactsContract.Contacts.CONTENT_URI; // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND (" + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND (" + ContactsContract.Contacts.DISPLAY_NAME + " != ‘‘ ))"; return new CursorLoader(ContactActivity.this.getApplicationContext(), baseUri, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); List<Map<String, String>> list = new ArrayList<>(); while (data.moveToNext()) { Map<String, String> map = new HashMap(); String name = data.getString(data.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String ContactId = data.getString(data.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null); while (phone.moveToNext()) { String Number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if (Number != null) { map.put(name, Number); } } list.add(map); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。