私のアプリケーションでは、電話番号を使用して連絡先を検索しようとしています。私が探している電話番号は常に同じ形式です(例えば、 '123456789')。 しかし、次のコードは私が期待したすべての連絡先を取得しません。 主な問題は電話機の電話番号の形式が異なることがあります。連絡先によっては「+12 345 6789」、もう1つには「」という名前が付けられています。 私はContactsContract.PhoneLookup.NORMALIZED_NUMBERを試しましたが、私のコードは '123456789'形式の電話番号で保存された連絡先だけを取得します。Android - 番号の異なる形式の連絡先を検索
private String getContactDetails(Context context, String number) {
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.LOOKUP_KEY};
int len = number.length();
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.substring(len-7)));
String selection = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
selection = ContactsContract.PhoneLookup.NORMALIZED_NUMBER + " LIKE %" + number.substring(len-7) + "%";
}
Cursor cursor = context.getContentResolver().query(contactUri, projection, selection, null, null);
String name = null;
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
return name;
}