我正在实现一个 SMS 应用程序,到目前为止,我实现了获取所有消息(发送、接收、草稿)及其联系号码、线程 ID、联系人 ID、日期、类型。
这是我的代码:
Uri mSmsinboxQueryUri = Uri.parse("content://sms");
Cursor cursor = _context.getContentResolver().query(
mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "date", "body",
"type" }, null, null, null);
String[] columns = new String[] { "address", "thread_id", "date",
"body", "type" };
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String address = null, date = null, msg = null, type = null, threadId = null;
address = cursor.getString(cursor.getColumnIndex(columns[0]));
threadId = cursor.getString(cursor.getColumnIndex(columns[1]));
date = cursor.getString(cursor.getColumnIndex(columns[2]));
msg = cursor.getString(cursor.getColumnIndex(columns[3]));
type = cursor.getString(cursor.getColumnIndex(columns[4]));
Log.e("SMS-inbox", "\nTHREAD_ID: "
+ threadId + "\nNUMBER: " + address + "\nTIME: " + date + "\nMESSAGE: " + msg + "\nTYPE: " + type);
}
}
}
现在,我需要按线程 ID(具有相同线程 ID 的消息)分隔这些消息。我怎样才能最好地做到这一点?谢谢!