在Android中的对话中阅读收件箱中的短信

时间:2015-04-15 13:12:57

标签: android sms messages

我正在开发一个消息传递应用程序,我希望将所有消息都放入对话列表中 我正在尝试下面的代码,但它只是阅读收件箱中的所有邮件,而不是对话

            Uri inboxURI = Uri.parse("content://sms/inbox");

            String[] reqCols = new String[] { "_id", "address", "body" };

            ContentResolver cr = getContentResolver();

            Cursor c = cr.query(inboxURI, reqCols, null, null, null);

            adapter = new SimpleCursorAdapter(context, R.layout.message_box_view, c,
                    new String[] { "address", "body" }, new int[] {
                    R.id.tv_message_box_number, R.id.tv_message_box_text }, 1);

            lv_messagesBox.setAdapter(adapter);

2 个答案:

答案 0 :(得分:1)

我正在使用此代码来获取收件箱 -

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
   String count = Integer.toString(cursor1.getCount());
   while (cursor1.moveToNext()){
       String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
       String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
       String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
       String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
       String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
    }
}

通过此URI获取发送的短信 -

Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");

希望这会有所帮助:)

答案 1 :(得分:1)

我解决了我的问题

    Uri uri = Uri.parse("content://sms/");
    String[] reqCols = new String[] { "_id", "body", "address", "read", "date", "type", "thread_id" };

    ContentResolver cr = context.getContentResolver();
    Cursor cursor = cr.query(uri, reqCols, "thread_id = '" + threadId + "'" , null, "date ASC");