根据电话号码查找通讯录中联系人姓名

方法一

参照google文档的说明进行的查找操作:

google文档query说明如下(第二条):

Query
  • If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.
  • If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.
  • If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.
  • If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data table. The result will contain contact ID, name etc.

public ArrayList<String> mobile2name(String mobile){
		String[] projection = new String[]{PhoneLookup.DISPLAY_NAME};
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(mobile));
		Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
		String name = "";
		ArrayList<String> names = new ArrayList<String>();
		if(cursor != null && cursor.moveToFirst()){
//			name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
			do{
				name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
				if(name != null && !"".equals(name)){
					names.add(name);
				}
			}while(cursor.moveToNext());
		}
		return names;
	}
------------------------------------------------------------------------------------------

方法二

这个方法是转自网络,也是可行的

转自:http://www.cnblogs.com/zdz8207/archive/2012/11/09/2762893.html

/*
      * 根据电话号码取得联系人姓名
      */
     public static String getContactNameByPhoneNumber(Context context, String address) {
         String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                 ContactsContract.CommonDataKinds.Phone.NUMBER };
 
         // 将自己添加到 msPeers 中
         Cursor cursor = context.getContentResolver().query(
                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                 projection, // Which columns to return.
                 ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
                         + address + "'", // WHERE clause.
                 null, // WHERE clause value substitution
                 null); // Sort order.
 
         if (cursor == null) {
             Log.d(TAG, "getPeople null");
             return null;
         }
         for (int i = 0; i < cursor.getCount(); i++) {
             cursor.moveToPosition(i);
 
             // 取得联系人名字
             int nameFieldColumnIndex = cursor
                     .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
             String name = cursor.getString(nameFieldColumnIndex);
             return name;
         }
         return null;
     }
 
 /**
      * 获取所有联系人内容
      * @param context
      * @param address
      * @return
      */
     public static String getContacts(Context context) {
         StringBuilder sb = new StringBuilder();
         
         ContentResolver cr = context.getContentResolver();
         Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                 null, null, null);
 
         if (cursor.moveToFirst()) {
             do {
                 String contactId = cursor.getString(cursor
                         .getColumnIndex(ContactsContract.Contacts._ID));
                 String name = cursor
                         .getString(cursor
                                 .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                 //第一条不用换行
                 if(sb.length() == 0){
                     sb.append(name);
                 }else{
                     sb.append("\n" + name);
                 }
                 
                 Cursor phones = cr.query(
                         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                         null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                 + " = " + contactId, null, null);
                 while (phones.moveToNext()) {
                     String phoneNumber = phones
                             .getString(phones
                                     .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     // 添加Phone的信息
                     sb.append("\t").append(phoneNumber);
                     
                 }
                 phones.close();
                 
             } while (cursor.moveToNext());
         }
         cursor.close();
         return sb.toString();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值