简体   繁体   English

检索具有电话号码(如已拨电话号码)的联系人列表

[英]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: 例如,如果我键入123,我想检索所有联系人号码以123开头的联系人。为此,我使用以下代码:

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 + "%'", 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. 这段代码的问题是,如果我保存了+ 919-9 ....和+9199 ....这样的联系人,则当我拨打+9199时无法检索到两个联系人。 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." “我的问题不在于拨打的号码。即使我拨打1234或123-4,我也需要所有电话号码以1234开始的联系人。但是,如果我键入1234,则仅检索以1234开头的联系人,而不是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: 那是因为您直接检查该数字。尝试使您的字符串成为DialledNumber,使其看起来像123-456-7890,然后查询这两个字符串,例如:

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. 这是一种骇客行为,但您可以使用它在其他任何地方都不会给您造成问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM