0

i want to retrieve the phone number from a contact name. so i have this code

public String getPhoneNumber(String name) {
    String phonenumber = "";
    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            "DISPLAY_NAME LIKE '%" + name + "%'", null, null);
    if (cursor.moveToFirst()) {
        String contactId = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));
        //
        // Get all phone numbers.
        //
        Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID
                + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones
                    .getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
            case Phone.TYPE_HOME:
                //phonenumber = number;
                // do something with the Home number here...
                break;
            case Phone.TYPE_MOBILE:
                phonenumber = number;
                // do something with the Mobile number here...
                break;
            case Phone.TYPE_WORK:
                //phonenumber = number;
                // do something with the Work number here...
                break;
            }
        }
        phones.close();
        //
        // Get all email addresses.
        //

    }
    cursor.close();
    return phonenumber;
}

But, if the contact name is not match exactly with name in db, it will return nothing. So whether there have any way to retrieve the phone number even the input name is not very likely to?

for example, there have an contact : Prototype Alex and my input name is: prototype or alex.

4

1 に答える 1

0

これはあなたの具体的な問題に対する答えではありませんが、役立つかもしれません。レーベンシュタイン距離、soundex、metaphone などの近似文字列マッチングのアルゴリズムがあります。おそらく、コンテンツ プロバイダーからすべての連絡先を取得してから、これらのアルゴリズムのいずれかを使用して検索を行うという選択肢があるかもしれません。

于 2012-04-16T13:41:36.860 に答える