I want to match a number in the contactlist i got the contactlist in my activity i tried to match the contactlist with a number that is stored in contactlist but i am not getting the result as per.... The code that i have used is...
String phoneNumber = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
String DATA = ContactsContract.CommonDataKinds.Email.DATA;
StringBuffer output = new StringBuffer();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null,
null);
// Loop for every contact in the phone
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor
.getString(cursor.getColumnIndex(_ID));
String name = cursor.getString(cursor
.getColumnIndex(DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
//output.append("\n First Name:" + name);
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(
PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
new String[] { contact_id }, null);
while (phoneCursor.moveToNext()) {
Toast.makeText(getApplicationContext(), number, Toast.LENGTH_SHORT).show();
//i am getting the incoming call number here, the toast is displayed
//String number="+919729226730";
phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndex(NUMBER));
if(number.equals(phoneNumber)){
Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show();
Log.i("phone", "number matched");
}
output.append("\n Phone number:" + phoneNumber);
}
phoneCursor.close();
}
output.append("\n");
}
change the order
change this:
if(number.equals(phoneNumber)){
Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show();
Log.i("phone", "number matched");
}
phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndex(NUMBER));
to
phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndex(NUMBER));
if(number.equals(phoneNumber)){
Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show();
Log.i("phone", "number matched");
}
Get the phoneNumber
first and then compare.