开发者

Retrieve a list of contacts having phone number like the dialed number

I'm trying to retrieve the phone contacts having phone number starting with the number being dialled. For eample if I type 123, I would like to retrieve all the contacts having contact number starting with 123. I'm using the following code for this:

Ur开发者_如何学JAVAi uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = this.getContentResolver().query(
        uri,
        new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
        ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'", null,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

The issue with this code is, if I have saved a contact like +919-9.... and another like +9199...., when I dial +9199 I can't retrieve both the contacts. I would like to escape the character "-" while querying the contacts. How could I do this? Please help. Thank you.


System.out.print("1-2-3".replaceAll("\\-", ""));


"My problem is not with the dialled number. Even if I dial 1234 or 123-4, I need all the contacts with phone number starting with 1234. But here if I type 1234 only contacts starting with 1234 is retrieved and not 123-4."

that is because you directly check for the number.try making your string dialledNumber such that it looks like 123-456-7890 then query for both this string like:

If suppose,

String dialledNumber="1234";
String dialledNumberFormatted="123-4"; // Create this on your own from dialledNumber you get

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor cursor = this.getContentResolver().query(
        uri,
        new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
        ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'" +" OR "+ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumberFormatted + "%'", null,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

This is a type of hack but you can use it doesn't cause problem for you anywhere else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜