3

我想从一个联系人那里获得所有消息。我有联系人 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)的消息,但我只能显示最后一条消息正文。所以我认为存在其他查询来获取来自一个用户的所有消息?

有任何想法吗 ???

4

2 回答 2

1

我怀疑_id在这种情况下是您发送短信/彩信的联系人的 ID,而是线程的 ID。您可能要用于查询子句的列是RECIPIENT_IDS。出于调试目的,尝试不使用 where 子句并转储光标以分析结果

Cursor cursor = cr.query(Uri.parse("content://mms-sms/conversations"), new String[]{"*"}, null , null, null);
DatabaseUtils.dumpCursor(cursor);
于 2015-10-03T09:37:01.640 回答
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列,我可以知道消息是由我发送的还是由我的联系人发送的。

于 2015-10-04T18:22:17.093 回答