如何检索最近5天的收件箱邮件?

时间:2014-08-21 11:27:57

标签: java android

您好我可以检索所有收件箱邮件,但我想检索最近5天的邮件如何实现此目的。

        private void readMessage(){
            Uri inboxURI = Uri.parse("content://sms/inbox");
            String[] reqCols = new String[] { "_id", "address", "body", "date" };
            ContentResolver cr = getContentResolver();
            Cursor c = cr.query(inboxURI, reqCols, null, null, null);

        }

<uses-permission android:name="android.permission.READ_SMS">

1 个答案:

答案 0 :(得分:1)

您需要在date日期列中使用where子句来检索所有数据的日期大于5天前。请查看下面的示例代码。

private static final long FIVE_DAYS_MILIS = 24 * 5 * 60 * 60 * 1000;

    private void readMessage() {
        Uri inboxURI = Uri.parse("content://sms/inbox");
        long currentTimeMilis = System.currentTimeMillis();
        // Time milis before 5 days
        long milisBefore5Days = currentTimeMilis - FIVE_DAYS_MILIS;
        // Where clause saying that date should be greater that milis before 5
        // days.
        String selection = "date > ?";
        String selectionArgs[] = { Long.toString(milisBefore5Days) };
        String[] reqCols = new String[] { "_id", "address", "body", "date" };
        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(inboxURI, reqCols, selection, selectionArgs, null);
    }

希望这有帮助!