最近有个关于通讯录开发的需求,需求很简单:根据手机号码查询姓名。之前有获取通讯录列表的代码如下:
/**
* 获取本机手机联系人列表
*
* @author yinbiao
* @date 2016-4-5 上午11:03:48
* @param context
* @return
*/
public synchronized static List<MocamContact> getLocalPhoneContacts(Context context) {
String[] projection = { Phone.DISPLAY_NAME, Phone.NUMBER };
List<MocamContact> list = new ArrayList<MocamContact>();
ContentResolver resolver = context.getContentResolver();
// 获取手机联系人
Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
// 得到手机号码
String phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
// 如果不是正確的手機號碼 跳过当前循环
if (!isMobileNomber(phoneNumber)) {
continue;
}
// 得到联系人名称
String name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));