Android 使用电话号码搜索联系人
我想检索与传入消息号码关联的联系人的姓名。 我想出了以下代码Android 使用电话号码搜索联系人,android,android-contacts,Android,Android Contacts,我想检索与传入消息号码关联的联系人的姓名。 我想出了以下代码 Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msgSender)); Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME},null,null,null); try {
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msgSender));
Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME},null,null,null);
try {
c.moveToFirst();
displayName = c.getString(0);
} catch (Exception e) {
// TODO: handle exception
}finally{
c.close();
}
问题是,它在emulator上工作,但在我的移动设备上不工作。我试着重新启动我的手机。但仍然不起作用。我搜索到与此类似的代码,但在实际手机上无法使用。您需要应用以下代码才能在android 2.1版的真实手机上使用
//String msgSender="Mobile No";
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msgSender));
Cursor c = getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null);
try {
c.moveToFirst();
String displayName = c.getString(0);
} catch (Exception e) {
// TODO: handle exception
}finally{
c.close();
}
在Android 2.1中,我们必须使用ContactsContract
,而且PhoneLookup
应该是ContactsContract
确保您已给予以下许可
<uses-permission android:name="android.permission.READ_CONTACTS"/>
您的read手机是什么版本的?在我的手机中,它正在HTC s上工作。