Android查找与Charcase无关的名称联系人

时间:2014-03-16 00:09:07

标签: java android char call contact

我想创建一个代码,如果联系人等于搜索,或者是小写或大小写的话,会找到联系人并给他打电话 这是我目前使用的代码

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        "DISPLAY_NAME = '" + "contactname" + "'", null, null);
if (cursor.moveToFirst()) {
    String contactId =
            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
        switch (type) {
            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+number));
                startActivity(callIntent);
                break;
        }
    }
    phones.close();
}

cursor.close();

现在代码只有在完全按照输入搜索的方式保存时才会找到联系人,我希望它找到与其字符大小写无关的联系人

2 个答案:

答案 0 :(得分:1)

您可以使用Java equalsIgnoreCase()方法在检索联系人姓名后比较String。

str1.equalsIgnoreCase(str2);

如果您要求Query执行此操作,请尝试此

UPPER(contactName)=UPPER(contactSearchText)

LOWER(contactName)=LOWER(contactSearchText)

答案 1 :(得分:1)

找到了一种自行修复的方法

String name = "contact_name_to_search_for"
    ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
        null,"DISPLAY_NAME" + " LIKE '%"+name+"%'", null,
        ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED + " DESC");
if (cursor.moveToFirst()) {
    String contactId =
            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)).toLowerCase();
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId.toLowerCase(), null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
        switch (type) {
            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                Toast.makeText(getApplicationContext(), number+" Home number",Toast.LENGTH_SHORT ).show();
                break;
            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                Toast.makeText(getApplicationContext(), number+" Mobile number",Toast.LENGTH_SHORT ).show();
                break;
            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                Toast.makeText(getApplicationContext(), number+" Work number",Toast.LENGTH_SHORT ).show();
                break;
        }
    }
    phones.close();
}
cursor.close();