|
由于之前people过时,就不能使用这样的方法了,就采用下面的方法: public void getUserInfo(){
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d(TAG , "Name is : "+name);
int isHas = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if(isHas>0){
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + id,null,null);
while(c.moveToNext()){
String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d(TAG , "Number is : "+number);
}
c.close();
}
}
cursor.close();
}
有时候也会遇到根据号码找联系人的情况,及时提示用户。 private String getNameFromPhone(String number) { String name = null; String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; Cursor cursor = this.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, // Which columns to return. ContactsContract.CommonDataKinds.Phone.NUMBER + " = '" + number + "'", // WHERE clause. null, // WHERE clause value substitution null); // Sort order. if (cursor == null) { Log.d(TAG, "getPeople null"); return null; } Log.d(TAG, "getPeople cursor.getCount() = " + cursor.getCount()); for (int i = 0; i < cursor.getCount(); i++) { cursor.moveToPosition(i); int nameFieldColumnIndex = cursor .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME); name = cursor.getString(nameFieldColumnIndex); Log.i(TAG, "" + name + " .... " + nameFieldColumnIndex); } cursor.close(); return name; } |
android 中查找获取联系人
最新推荐文章于 2017-10-28 12:42:27 发布
本文介绍了一种在Android中获取设备上保存的联系人信息的方法,包括读取联系人姓名和电话号码,并提供了如何根据电话号码查找对应联系人的示例代码。
6430

被折叠的 条评论
为什么被折叠?



