由于之前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系统API获取联系人姓名和电话号码,包括遍历联系人列表、查询特定联系人的详细信息和根据电话号码查找联系人名。
&spm=1001.2101.3001.5002&articleId=8111397&d=1&t=3&u=ae36649fc2294998944b81209d294b33)
451

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



