如何从android手机号码获得昵称 [英] How to get nick name from phone number in android

查看:111
本文介绍了如何从android手机号码获得昵称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2174048/how-to-look-up-a-contacts-name-from-their-phone-number-on-android">How要查找联系人的名字从Android上的电话号码吗?

如何获得的昵称从电话号码android.What是用于内容提供商。

How to get nick name from phone number in android.What are content provider used.

推荐答案

使用下面的code:

String [] projection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
Cursor cursor =
    this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                                    projection, null, null, null);
if (cursor.moveToNext()) {
    int displayNameIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    String displayName = cursor.getString(displayNameIdx);
}

我不完全相信你所需要的显示名称,但你可以得到进一步的细节,如果你使用ContactsContract.CommonDataKinds.StructuredName:

I am not entirely sure you need the display name, but you can get further details if you get to use ContactsContract.CommonDataKinds.StructuredName:

String [] projection = { ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                         ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME };
String where = ContactsContract.Data.CONTACT_ID
               + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
String[] whereParameters =
    { contactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor cursor =
        this.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                projection, where, whereParameters, null);

编辑:嗯,现在我看到你想要映射的昵称在电话号码中。这是你如何选择CONTACT_ID出给定的电话号码:

Ah and now as I see you want to map the nickname to a phone number. This is how you select the contact_id out of given phone number:

String [] projection =
    { ContactsContract.CommonDataKinds.Phone.CONTACT_ID };
Cursor phoneCursor =
        this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", 
                new String[]{number}, null);
int idIndex = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
while (phoneCursor.moveToNext()) { 
    String id = phoneCursor.getString(numberIndex);
    Log.i(LOG_TAG, "This is the contact id associatated with the given number" + id);
} 
phoneCursor.close();

这篇关于如何从android手机号码获得昵称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆