如何获取短信的接收时间

How to get the time of receiving of an sms

我正在通过以下代码读取来自特定号码的未读短信。

public void getUnreadMessage() {
        Cursor smsInboxCursor1 = getContentResolver().query(
                Uri.parse("content://sms/inbox"), new String[] {},
                "read = 0 and address='" + pre_address + "'", null, null);
        int indexBody = smsInboxCursor1.getColumnIndex("body");
        int indexAddress = smsInboxCursor1.getColumnIndex("address");
        if (indexBody < 0 || !smsInboxCursor1.moveToFirst())
            return;
    //  arrayAdapter.clear();
        do {

            String str = "SMS From: " + smsInboxCursor1.getString(indexAddress)
                    + "\n" + smsInboxCursor1.getString(indexBody) + " \n";
            fromNumber = smsInboxCursor1.getString(indexAddress);
            smsBody.add(smsInboxCursor1.getString(indexBody));
            // arrayAdapter.add(str);
            status.add(false);

        } while (smsInboxCursor1.moveToNext());
    }

现在我想获取从这个特定号码接收短信的时间。我怎样才能做到这一点 ?

有一个名为 DATE 的列,其中包含收到消息的日期。可以像其他已经检索的字段一样直接获取:

int indexData = smsInboxCursor1.getColumnIndex("data");

...

long dateReceived = smsInboxCursor1.getLong(indexData);

由于它是时间戳,因此您需要将其转换为人类可读的字符串。您可以使用此代码来完成:

private String getDate(long time) {
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time);
    String date = DateFormat.format("dd-MM-yyyy HH:mm:ss", cal).toString();
    return date;
}