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ます。

threadIdこれで、目的のすべてのメッセージを別のメッセージにコピーしてList、たとえば日付で並べ替えることができます。それはあなたがそれで何をしたいかによります。

于 2013-01-26T16:30:31.030 に答える