我想将来自联系人的电话号码与来电中的电话号码进行比较。 电话号码可以是欧洲的所有内容:
除了+之外,我删除所有非数字的内容。所以在欧洲我只需将00xxx或0xxxxx替换为+ 41xxxx。与世界其他地方有什么关系? 以下功能中的传入电话号码是否始终为+41124785641? API 9中的每个设备的代码是否正常?
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");
}
}
这是带有incomingNumber的onCallStateChanged:
public void onCallStateChanged(int state, String incomingNumber)
答案 0 :(得分:0)
我只是检查号码是否在联系人列表中。这是我的问题的最佳解决方案,所以我不必关心格式。
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;
}
}