首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取特定日期从特定号码收到的最后5条短信

读取特定日期从特定号码收到的最后5条短信
EN

Stack Overflow用户
提问于 2016-08-04 05:43:12
回答 1查看 1.3K关注 0票数 0

我想知道如何阅读最后五条短信从一个特定的移动号码收到在一个特定的日期。

我知道如何从一个特定的发送者读取所有的短信,以及如何读取最后的短信,但我无法获取和阅读最后几条短信。我试着用

代码语言:javascript
复制
"date DESC LIMIT 5"

我的代码如下所示

代码语言:javascript
复制
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] projection = {"address", "body"};
Cursor cursor1 = MainActivity.this.getContentResolver().query(mSmsinboxQueryUri,
                                                              null,
                                                              "address = ?",
                                                              new String[]{phoneNumber},
                                                              "date DESC LIMIT 5");

if (cursor1 != null && cursor1.moveToFirst()) {
    body = cursor1.getString(cursor1.getColumnIndex("body"));
    totalBody = totalBody + body;
    Log.d("Registration", totalBody);
}

但每次它只显示最后一条信息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-05 11:53:08

您只看到一条消息,因为您的代码只处理返回的Cursor中的第一条记录。您需要遍历Cursor来处理剩下的部分。例如:

代码语言:javascript
复制
if (cursor != null && cursor.moveToFirst()) {
    do {
        body = cursor1.getString(cursor1.getColumnIndex("body"));
        totalBody = totalBody + body;
        Log.d("Registration", totalBody);
    } while (cursor.moveToNext());
}

此外,如果希望将查询限制为某一天,可以使用Calendar来计算当天的开始和结束时间,以毫秒为单位--因为这是在SMS表中存储日期的方式--并向where子句添加适当的比较。例如:

代码语言:javascript
复制
private static final int DAY_MILLISECONDS = 24 * 60 * 60 * 1000;
private static final Uri inboxUri = Uri.parse("content://sms/inbox");

// Months are zero-based; i.e., JANUARY == 0
// Phone number must be exact in this example
private void listMessages(String phoneNumber, int year, int month, int day) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DATE, day);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    String[] projection = {"address", "body"};
    String whereAddress = "address = ?";
    String whereDate = "date BETWEEN " + cal.getTimeInMillis() +
                       " AND " + (cal.getTimeInMillis() + DAY_MILLISECONDS);
    String where = DatabaseUtils.concatenateWhere(whereAddress, whereDate);

    Cursor cursor = null;
    try {
        cursor = getContentResolver().query(inboxUri,
                                            projection,
                                            where,
                                            new String[]{phoneNumber},
                                            "date DESC LIMIT 5");

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Log.d("Message", cursor.getString(cursor.getColumnIndex("body")));
            } while (cursor.moveToNext());
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38759270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档