Android短信,按其线程ID分组

时间:2013-01-26 16:15:26

标签: android android-contentprovider

我正在实施一个短信应用程序,直到现在我已经实现了所有消息(发送,接收,草稿)及其联系号码,线程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的消息)来分隔这些消息。我怎样才能做到最好?谢谢!

1 个答案:

答案 0 :(得分: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")); 
    }
}

现在,您可以在SmsMsgcursor.moveToNext() true的情况下在while循环中实例化List的对象,并将其添加到您选择的threadId

您现在可以将所需的List的所有邮件复制到其他{{1}},并按日期对其进行排序。这取决于你想要做什么。