如何通过在android中发送联系号码来获取联系人姓名?

时间:2012-02-17 04:44:10

标签: android android-contacts

如果有任何方法通过在android.if中发送联系人号码获取联系人姓名,任何人都有想法。

private String getContactName(String string) {
         String name=null;
          ContentResolver cr = getContentResolver();
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null, null);
            if (cur.getCount() > 0) {
             while (cur.moveToNext()) {
                 String id =  cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                  name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
             }
               } 
        return name;
    }

我正在向这些方法发送联系电话。如何获取联系人姓名。

3 个答案:

答案 0 :(得分:1)

使用以下方法与联系提供商联系:

Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(string));

Cursor cursor = mContext.getContentResolver().query(contactUri, null, null, null, null);

此游标的结果集编号与字符串相同。

答案 1 :(得分:1)

我是这样做的。

public static String getContactName(final String phoneNumber,Context context) {
        Uri uri;
        String[] projection;
        Uri mBaseUri = Contacts.Phones.CONTENT_FILTER_URL;
        projection = new String[] { android.provider.Contacts.People.NAME };
        try {
            Class<?> c = Class
                    .forName("android.provider.ContactsContract$PhoneLookup");
            mBaseUri = (Uri) c.getField("CONTENT_FILTER_URI").get(mBaseUri);
            projection = new String[] { "display_name" };
        } catch (Exception e) {
        }
        uri = Uri.withAppendedPath(mBaseUri, Uri.encode(phoneNumber));
        Cursor cursor = context.getContentResolver().query(uri, projection, null,
                null, null);

        String contactName = "";

        if (cursor.moveToFirst()) {
            contactName = cursor.getString(0);
        }
        cursor.close();
        cursor = null;
        return contactName;
    }

答案 2 :(得分:0)

尝试

Uri uri;
String[] projection;
Uri baseUri = Contacts.Phones.CONTENT_FILTER_URL;
projection = new String[] { android.provider.Contacts.People.NAME };
try {
Class<?> c = Class.forName("android.provider.ContactsContract$PhoneLookup");
baseUri = (Uri) c.getField("CONTENT_FILTER_URI").get(baseUri);
projection = new String[] { "display_name" };
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (SecurityException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}
uri = Uri.withAppendedPath(baseUri, Uri.encode(Uri
            .encode(phoneNumber)));
Cursor cursor =mContext.getContentResolver().query(uri,
            projection, null, null, null) ;

String fromDisplayName = null;
if (cursor != null) {
if (cursor.moveToFirst()){
    fromDisplayName = cursor.getString(0);
}
else{
    fromDisplayName="";
}
cursor.close();
} else {
    fromDisplayName="";
}
return fromDisplayName;

不推荐使用Uri android.provider.Contacts.People.NAME。但android.provider.ContactsContract $ PhoneLookup仅可从API级别5获得。因此,最好使用反射来支持所有手机。