我想从一个联系人处获取所有消息。我有联系人ID _id
,所以我已经做过类似的事情:
private void displayMessages() {
ContentResolver cr = this.getContentResolver();
try {
Cursor cursor = cr.query(Uri.parse("content://mms-sms/conversations"), new String[]{"*"}, this.contID + "= _id" , null, null); //contID is the unique ID for one contact in our android.
while (cursor.moveToNext()) {
System.out.println("Number: " + cursor.getString(cursor.getColumnIndexOrThrow("address")));
System.out.println("Body : " + cursor.getString(cursor.getColumnIndexOrThrow("body")));
}
cursor.close();
}
catch (Exception e) {
System.out.print("ERROR");
}
我问过像
这样的话SELECT * FROM content://mms-sms/conversations WHERE _id = contID
正如您所看到的,在我的查询中,我向系统询问来自一个联系人的消息(我知道的用户ID)但我可以显示最后一条消息正文。所以我认为其他查询是否存在来自一个用户的所有消息?
任何想法???
答案 0 :(得分:1)
我怀疑_id
在这种情况下是你发送sms / mms的联系人的id,而是线程的id。您可能要用于查询子句的列是RECIPIENT_IDS。出于调试目的尝试不使用where子句并转储光标以分析结果
Cursor cursor = cr.query(Uri.parse("content://mms-sms/conversations"), new String[]{"*"}, null , null, null);
DatabaseUtils.dumpCursor(cursor);
答案 1 :(得分:1)
很抱歉回答太晚,但我找到了解决方案。
这是我的代码:
private void displayMessages() {
ContentResolver cr = this.getContentResolver();
boolean isMe;
try {
Cursor cursor = cr.query(Uri.parse("content://sms"), null, this.cont.getThread_ID() + " = thread_id" , null, "date ASC");
while (cursor.moveToNext()) {
//if it's a received message :
if ((cursor.getString(cursor.getColumnIndexOrThrow("person")) != null)) {
isMe = false;
}
else {
//it's a message sent by me.
isMe = true;
}
date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
System.out.println("Number: " + cursor.getString(cursor.getColumnIndexOrThrow("address")));
System.out.println("Body : " + cursor.getString(cursor.getColumnIndexOrThrow("body") + "\n");
}
cursor.close();
}
感谢“conent:// sms”表,我可以获得所有短信! 所以我使用 thread_id 来显示所有消息,并且由于 person 列,我可以知道消息是由我或我的联系人发送的。