Noa*_*man 49 android phone-number contactscontract android-contacts
我想检索与传入电话号码关联的联系人姓名.当我处理broascastreceiver中的传入号码时,有一个带有传入呼叫者姓名的字符串将极大地帮助我的项目.
我认为这涉及使用sql WHERE子句作为过滤器的查询,但是我是否需要对联系人进行排序?一个例子或提示将是非常有帮助的.
Vik*_*exe 86
虽然这已经得到了解答,但这里是从数字中获取联系人姓名的完整功能.希望它能帮助别人:
public static String getContactName(Context context, String phoneNumber) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return null;
}
String contactName = null;
if(cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if(cursor != null && !cursor.isClosed()) {
cursor.close();
}
return contactName;
}
Run Code Online (Sandbox Code Playgroud)
[ 根据Marcus的评论更新 ]
你必须要求这个许可:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Pen*_*m10 77
为此,您需要使用优化的PhoneLookup提供程序,如上所述.
将权限添加到AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Run Code Online (Sandbox Code Playgroud)
然后:
public String getContactName(final String phoneNumber, Context context)
{
Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
String contactName="";
Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null);
if (cursor != null) {
if(cursor.moveToFirst()) {
contactName=cursor.getString(0);
}
cursor.close();
}
return contactName;
}
Run Code Online (Sandbox Code Playgroud)
mda*_*ddy 25
这非常有用,这是我检索调用者姓名,ID和照片的最终代码:
private void uploadContactPhoto(Context context, String number) {
Log.v("ffnet", "Started uploadcontactphoto...");
String name = null;
String contactId = null;
InputStream input = null;
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
if (cursor.moveToFirst()) {
// Get values from contacts database:
contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
// Get photo of contactId as input stream:
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
Log.v("ffnet", "Started uploadcontactphoto: Contact Found @ " + number);
Log.v("ffnet", "Started uploadcontactphoto: Contact name = " + name);
Log.v("ffnet", "Started uploadcontactphoto: Contact id = " + contactId);
} else {
Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found @ " + number);
return; // contact not found
}
// Only continue if we found a valid contact photo:
if (input == null) {
Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name);
return; // no photo
} else {
this.type = contactId;
Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name);
}
Run Code Online (Sandbox Code Playgroud)
...然后用"输入"(他们的照片作为InputStream),"name"和"contactId"做任何你想做的事.
以下是列出您可以访问的约15个左右列的文档,只需将它们添加到上面代码开头附近的投影中:http: //developer.android.com/reference/android/provider/ContactsContract. PhoneLookup.html
| 归档时间: |
|
| 查看次数: |
59110 次 |
| 最近记录: |