0

我想在指定日期之后获取设备的消息(发送和接收) 。我正在使用以下逻辑来获取消息,但我无法为 where 条件编写逻辑。我该如何解决这个问题?

Uri uriSMSURI = Uri.parse("content://sms/");
String WHERE_CONDITION="";
//Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

Cursor cur = getContentResolver().query(uriSMSURI,
new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                 WHERE_CONDITION,
                 null, null);

while (cur.moveToNext()) {
    address.add(cur.getString(cur.getColumnIndex("address")));
    body.add(cur.getString(cur.getColumnIndex("body")));
    id.add(cur.getString(cur.getColumnIndex("_id")));
    date.add(cur.getString(cur.getColumnIndex("date")));
}
4

2 回答 2

2

您可以使用一个java.util.Date对象:

Date date = new Date(113,9,1,0,0,0);  // 2013/10/01 00:00:00
long time = date.getTime();
Uri uriSMSURI = Uri.parse("content://sms/");
String WHERE_CONDITION="date >= ?";

Cursor cur = getContentResolver().query(uriSMSURI,
new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                 WHERE_CONDITION,
                 new String[] { Long.toString(time) }, null);

        while (cur.moveToNext()) {
        address.add(cur.getString(cur.getColumnIndex("address")));
        body.add(cur.getString(cur.getColumnIndex("body")));
        id.add(cur.getString(cur.getColumnIndex("_id")));
        date.add(cur.getString(cur.getColumnIndex("date")));

            }
于 2013-10-01T18:19:25.440 回答
2

以下是您查询的步骤。

date.add(cur.getString(cur.getColumnIndex("date")));

此代码以长格式返回日期。

1:-将此长代码转换为正确的日期格式。

long dateInLong=long.parseLong(date.add(cur.getString(cur.getColumnIndex("date"))));
String msgDate=DateFormat.format("MM/dd/yyyy",dateInLong).toString;

2:-比较你的日期和这个msgDate并显示符合要求的消息

SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy");
Date d1= sdf.parse(msgDate);
Date d2=sdf.parse("your date in MM/dd/yyyy format");
if(d1.compareTo(d2)>0){
Log.e("","d1 is after d2");
}
else if(d1.compareTo(d2)<0){
Log.e("","d1 is before d2");
}
else
{
 Log.e("","both dates are same");
}

而已。:)

于 2014-03-14T08:05:18.690 回答