如何使用类似于android中的选择从数字中获取联系人姓名

San*_*ndo 1 android

这是我使用的代码

    private String getContactNameFromNumber(String number) {
        // define the columns I want the query to return
        String[] projection = new String[] {
                Contacts.Phones.DISPLAY_NAME,
                Contacts.Phones.NUMBER };

        // encode the phone number and build the filter URI
        Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));

        // query time
        Cursor c = getContentResolver().query(contactUri, projection, null,
                null, null);

        // if the query returns 1 or more results
        // return the first result
        if (c.moveToFirst()) {
            String name = c.getString(c
                    .getColumnIndex(Contacts.Phones.DISPLAY_NAME));
            return name;
        }

        // return the original number if no match was found
        return number;
    }
Run Code Online (Sandbox Code Playgroud)

但此代码仅返回完全等于联系号码的数字.我想使用like语句,以便即使最后7个数字匹配我应该能够得到名称..如何写...?

小智 7

使用电话号码从电话联系人中检索姓名

private String getContactNameFromNumber(String number) { 
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));


    Cursor cursor = context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
    if (cursor.moveToFirst())
    {
        name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }   


    return name;
    //proceed as you need 

} 
Run Code Online (Sandbox Code Playgroud)