contact exists in contacts

后端 未结 3 2037
予麋鹿
予麋鹿 2021-01-02 17:27

I have phone number. Is there any way to check whether the phone number exists in contacts database in the device or not? Depending on that I need have move further in my ap

3条回答
  •  清歌不尽
    2021-01-02 18:10

    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.NUMBER, PhoneLookup.DISPLAY_NAME };
            Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
            try {
                if (cur.moveToFirst()) {
                    return true;
                }
            } finally {
                if (cur != null)
                    cur.close();
            }
            return false;
        } else {
            return false;
        }
    }// contactExists
    

    Handled nullpointer exception.

提交回复
热议问题