我有电话号码.有没有办法检查设备中的联系人数据库中是否存在电话号码?根据我的需要,我的应用程序进一步移动.请建议或者如果有任何人可以提供示例代码段,请提供.
以下是我写的代码:
public boolean contactExists(Activity _activity,String number) { String[] mPhoneNumberProjection = { PhoneLookup._ID,PhoneLookup.NUMBER,PhoneLookup.DISPLAY_NAME }; Cursor cur = _activity.getContentResolver().query(number,mPhoneNumberProjection,null,null); try { if (cur.moveToFirst()) { return true; } } finally { if (cur != null) cur.close(); } return false; }// contactExists
提前致谢…
解决方法
public boolean contactExists(Activity _activity,String number) { if (number != null) { Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(number)); String[] mPhoneNumberProjection = { PhoneLookup._ID,PhoneLookup.DISPLAY_NAME }; Cursor cur = _activity.getContentResolver().query(lookupUri,null); try { if (cur.moveToFirst()) { return true; } } finally { if (cur != null) cur.close(); } return false; } else { return false; } }// contactExists
处理nullpointer异常.