将 Contact Phone 号码与来自 onCallStateChanged 的​​来电号码进行比较

Compare Contact Phone number with incomming number from onCallStateChanged

我想比较来自联系人的 phone 号码和来自来电的号码。 phone 数字可以是欧洲的以下任何内容:

我删除了除 + 之外的所有非数字。所以在欧洲,我只需要将 00xxx 或 0xxxxx 替换为 +41xxxx。世界其他地方怎么了? 以下函数中传入的 Phone 数字是否总是带有 +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)

我只是检查该号码是否在联系人列表中。这是我的问题的最佳解决方案,因此我不必关心格式。

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;
        }
    }