获取联系人姓名给出的Android电话号码 [英] Get contact name given a phone number in Android
本文介绍了获取联系人姓名给出的Android电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想给出的联系电话号码检索联系人姓名。我做了应该适用于所有的API版本的函数,通过我不能使它在1.6工作,我看不出这个问题,也许有人能发现吗?
请注意,我把它换成了API常量字符串,所以我没有去precated警告的问题。
公共字符串getContactName(最后弦乐phoneNumber的)
{
乌里URI;
的String []投影;
如果(Build.VERSION.SDK_INT> = 5)
{
URI = Uri.parse(内容://com.android.contacts/phone_lookup);
投影=新的String [] {DISPLAY_NAME};
}
其他
{
URI = Uri.parse(内容://联系人/手机/过滤器);
投影=新的String [] {姓名};
}
URI = Uri.withAppendedPath(URI,Uri.en code(phoneNumber的));
光标光标= this.getContentResolver()查询(URI,投影,NULL,NULL,NULL);
字符串联系人姓名=;
如果(cursor.moveToFirst())
{
CONTACTNAME = cursor.getString(0);
}
cursor.close();
光标= NULL;
返回联系人姓名;
}
解决方案
使用,而不是比较SDK版本的思考。
公共字符串getContactName(最后弦乐phoneNumber的)
{
乌里URI;
的String []投影;
mBaseUri = Contacts.Phones.CONTENT_FILTER_URL;
投影=新的String [] {android.provider.Contacts.People.NAME};
尝试 {
类<> C =的Class.forName(android.provider.ContactsContract $ PhoneLookup);
mBaseUri =(URI)c.getField(CONTENT_FILTER_URI)获得(mBaseUri)。
投影=新的String [] {DISPLAY_NAME};
}
赶上(例外五){
}
URI = Uri.withAppendedPath(mBaseUri,Uri.en code(phoneNumber的));
光标光标= this.getContentResolver()查询(URI,投影,NULL,NULL,NULL);
字符串联系人姓名=;
如果(cursor.moveToFirst())
{
CONTACTNAME = cursor.getString(0);
}
cursor.close();
光标= NULL;
返回联系人姓名;
}
I am trying to retrieve contact names given the contact phone number. I made a function that should work in all API versions, by I can't make it work in 1.6 and I can't see the problem, maybe someone can spot it?
Note that, I've replaced the API constants for strings so I don't have deprecated warning problems.
public String getContactName(final String phoneNumber)
{
Uri uri;
String[] projection;
if (Build.VERSION.SDK_INT >= 5)
{
uri = Uri.parse("content://com.android.contacts/phone_lookup");
projection = new String[] { "display_name" };
}
else
{
uri = Uri.parse("content://contacts/phones/filter");
projection = new String[] { "name" };
}
uri = Uri.withAppendedPath(uri, Uri.encode(phoneNumber));
Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null);
String contactName = "";
if (cursor.moveToFirst())
{
contactName = cursor.getString(0);
}
cursor.close();
cursor = null;
return contactName;
}
解决方案
Use reflections instead of comparing sdk version.
public String getContactName(final String phoneNumber)
{
Uri uri;
String[] projection;
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 = this.getContentResolver().query(uri, projection, null, null, null);
String contactName = "";
if (cursor.moveToFirst())
{
contactName = cursor.getString(0);
}
cursor.close();
cursor = null;
return contactName;
}
这篇关于获取联系人姓名给出的Android电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文