1

给定一个电话号码作为字符串,我如何找到它存储在联系人中的正确值?

示例:
给定电话号码:9743343954
联系人中的电话号码:+919743343954

编辑:数字的长度不固定。

谢谢。

4

5 回答 5

3

我不确定我是否理解您的意思,但也许这会对您有所帮助:

    //Find contact by given number
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("9743343954"));
    String[] projection = new String[] { PhoneLookup.NUMBER, PhoneLookup.NORMALIZED_NUMBER };
    Cursor c = getContentResolver().query(uri, projection, null, null, null);
    if (c.moveToFirst()) {// while(c.moveToNext()){
        //get number assigned by user to given contact, in this case 9743343954
        String number = c.getString(c.getColumnIndexOrThrow(PhoneLookup.NUMBER));
        //get normalized E164 number, in this case +919743343954
        String normalized = c.getString(c.getColumnIndexOrThrow(PhoneLookup.NORMALIZED_NUMBER));
        Toast.makeText(getApplicationContext(), "Number: " + number + "; normalized: " + normalized,
                Toast.LENGTH_LONG).show();
    }
    c.close();

要使其正常工作,请向项目清单添加权限:

    <uses-permission android:name="android.permission.READ_CONTACTS" />
于 2013-03-02T16:06:16.267 回答
2

使用您的示例,电话号码的长度为 10 个字符,在联系人中为 13 个字符,并且您希望其中的最后 10 个字符匹配。所以,像:

// I am assuming that you've removed all spaces, full stops,
// dashes etc from the two input variables
public boolean numbersMatch(String numberToMatch, String numberFromContacts) {
    // Ensure that the both numbers are of a reasonable length
    if (numberToMatch.length() < 9) return false;
    if (numberFromContacts.length() < 9) return false;

    // Is the number to match hidden in the contacts number
    if (numberFromContacts.lastIndexOf(numberToMatch) != -1) return true;

    // Or is the contact number hidden in the number to macth
    if (numberToMatch.lastIndexOf(numberFromContacts) != -1) return true;

    // No match, so return false
    return false;
}
于 2013-03-02T14:43:08.753 回答
1

你为什么不这样做

假设给定的电话号码是

String a = "9123456789";

联系人中的一个是

String b = "+91-9123456789";

然后您可以通过这种方式轻松检查

if(b.contains(a))
{
//Do what you want!
}
于 2013-03-02T15:41:30.153 回答
1

如果您想获取关联的联系人(或相关信息),@Lecho 的答案似乎很好。此页面上的更多信息来自 android 文档http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html

据我所知,PhoneLookup.NORMALIZED_NUMBER 从 API 16 开始可用。

另一方面,如果您想比较 2 个数字以查看它们是否相同但以两种不同的方式格式化,您可以使用:

PhoneNumberUtils.compare(phoneNumber1, phoneNumber2)

最好的,

于 2013-12-20T11:56:22.867 回答
-1

第一步是删除所有多余的空格和多余的字符:

//Removes the extra characters from the incoming number
    private String removeExtraCharacters(String phoneNumber){
        try
        {
            phoneNumber = phoneNumber.replace("(", "");
            phoneNumber = phoneNumber.replace(")", "");
            phoneNumber = phoneNumber.replace("-", "");
            phoneNumber = phoneNumber.replace(" ", "");
            phoneNumber = phoneNumber.replace("+", "");

        }
        catch (Exception e) 
        {
            Log.e("Cal Reciever_R", e.toString());
        }
        return phoneNumber;
    }

现在您可以使用 substring 方法获取 10 位数字并忽略前面的 +91,如下所示:

phoneNumber =phoneNumber .substring(phoneNumber .length()-10);
于 2013-03-02T15:28:44.440 回答