我希望能够提取当前存储在设备上默认短信应用程序中的短信(短信)对话的联系人的联系信息(姓名和号码)。这样做的最佳方式是什么?
答案 0 :(得分:1)
你可能想要查询下面的uri来获取所有在sms和mms中解决的地址
content://mms-sms/conversations
您需要获得address
列
ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, null, null, null);
String phone = "";
while(query.moveToNext()){
phone = query.getString(query.getColumnIndex("address"));
Log.d("test",phone);
}
编辑: 您可以查看从here复制的以下功能。将从上述地址栏中选取的号码传递给此功能
private String getContactNameFromNumber(String number) {
// define the columns I want the query to return
String[] projection = new String[] {
Contacts.Phones.DISPLAY_NAME,
Contacts.Phones.NUMBER };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));
// query time
Cursor c = getContentResolver().query(contactUri, projection, null,
null, null);
// if the query returns 1 or more results
// return the first result
if (c.moveToFirst()) {
String name = c.getString(c
.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
c.close();
return name;
}
c.close();
// return the original number if no match was found
return number;
}
您必须对此进行编辑以匹配您的查询,因为为每个数字调用此选项将会很慢。