I want to compare a phone number from the contacts with the one from an incoming call. The phone number can be everything like following in Europe:
I remove everything non numeric except the +. So in europe I just have to replace the 00xxx or 0xxxxx to +41xxxx. Whats with the rest of the world? Is the incoming Phone number in following function always something with +41124785641? Is the code ok for every device from API 9 on?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Integer i = 0;
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
String phoneNumber[] = new String[20];
while (cursor.moveToNext()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Log.d("onActivityResult : ", " "+contactId);
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
Log.d("onActivityResult : ", " "+hasPhone);
if ((Integer.parseInt(hasPhone)==1 )){
Log.d("onActivityResult : ", " it has a phone number");
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
phoneNumber[i] = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
i++;
}
phones.close();
}
Log.d("Hello : ", " "+name);
}
switch (requestCode) {
case PICKER_1:
String tmp[] = new String[i];
for(int j=0; j<i; j++){
tmp[j] = phoneNumber[j].replaceAll("[^\\d+]", ""); // removes all non numeric except +
Log.d("HELLO 1 : ", " "+tmp[j]);
}
break;
}
} else {
Log.w(LOG_TAG, "Warning: activity result is not ok");
}
}
here is the onCallStateChanged with the incomingNumber:
public void onCallStateChanged(int state, String incomingNumber)
I just check if the number is in the contacts list. Thats the best solution for my problem so I dont have to care about the format.
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE" + incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK" + incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING" + incomingNumber);
String result = null;
try {
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incomingNumber));
Cursor c = resolver.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (c != null) { // number is found
if (c.moveToFirst()) {
result = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.d("DEBUG", "RINGING " + result);
}
c.close();
}
} catch (Exception ex) {
Log.d("DEBUG", "catch ");
}
Toast toast = Toast.makeText(context.getApplicationContext(), incomingNumber + " Name: " +result, Toast.LENGTH_LONG);
toast.show();;
break;
}
}