且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:检查联系人列表中是否存在电话号码? (从电话中检索电话号码)

更新时间:2022-06-07 23:21:28

public boolean contactExists(Context context, String number) {
   /// number is the phone number
   Uri lookupUri = Uri.withAppendedPath(
   PhoneLookup.CONTENT_FILTER_URI, 
   Uri.encode(number));
   String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
   Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
   try {
      if (cur.moveToFirst()) {
         cur.close();
         return true;
   }
   } finally {
   if (cur != null)
      cur.close();
   }
   return false;
}