我在谈论这个话题Android get contact id from thread id,其中有人和我提出的问题完全相同。不幸的是,lopez.mikhael完全没有回答目标。
如何从URI content://sms/conversations
获取带有thread_id的联系人ID?假设它返回:
- thread_id
- snippet
- msg_count
获取所有联系人没有帮助。我从getColumnsNames()
那里搜索过,我没有看到任何包含thread_id的好列名。
为什么没有记录?是否弃用content://sms/
?还有其他解决方案吗?
谢谢。
答案 0 :(得分:2)
正如此post
所述您可以获取短信收件箱:
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String where = "thread_id="+ <thread id you get it from content://sms/conversations>;
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, where, null, null);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
while (cursor1.moveToNext()){
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
}
}
您可以通过更改URI来获取其他已发送的项目。
Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");
如果您有电话号码,可以使用以下代码找到联系人ID
String contactid = null;
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));
Cursor cursor = contentResolver.query(
uri,
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
if (contactid == null) {
Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}else{
//You can contact id do what you want to do with it.
}