2

我正在实现一个 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 的消息)分隔这些消息。我怎样才能最好地做到这一点?谢谢!

4

1 回答 1

2

首先,我不会单独保存这些字符串。

我会做的是这样的课程:

public class SmsMsg {
    private String address = null;
    private String threadId = null;
    private String date = null;
    private String msg = null;
    private String type = null;

    //c'tor
    public SmsMsg(Cursor cursor) {
       this.address = cursor.getString(cursor.getColumnIndex("address"));
       this.threadId = cursor.getString(cursor.getColumnIndex("thread_id"));
       this.date = cursor.getString(cursor.getColumnIndex("date"));
       this.msg = cursor.getString(cursor.getColumnIndex("body"));
       this.type = cursor.getString(cursor.getColumnIndex("type")); 
    }
}

SmsMsg现在您可以在您的 while 循环中cursor.moveToNext()实例化一个对象,true并将其添加到List您选择的 a 中。

例如,您现在可以将所需的所有消息复制threadId到不同的消息List并按日期对其进行排序。这取决于你想用它做什么。

于 2013-01-26T16:30:31.030 回答