说我正在访问Android的短信数据库:
Uri uri = Uri.parse("content://sms/");
ContentResolver contentResolver = getContentResolver();
String[] projection = {"_id", "date"};
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
我想选择“日期”大于参考日期的行。我知道我需要使用选择&选择参数:
String selection = "date > ?";
long referenceDateMilli = SOME_VALUE;
//how to define select argument ?
String selectionArgument = ?
Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgument, null);
定义日期参数值的最佳方法是什么?
答案 0 :(得分:0)
日期以毫秒为单位存储在SMS表中,因此您只需确定给定日期和时间的值,并查询任何更大的值。获得日期值的最简单方法可能是使用Calendar
。使用您给定的代码,以下内容将返回自年初以来创建的所有消息(给予或接受一秒)。
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
String[] selectionArgument = { String.valueOf(calendar.getTimeInMillis()) };
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgument, null);
答案 1 :(得分:0)
如果您发送数据,则datetime以毫秒为单位定义,因此您可以简单地使用
Calendar c = Calendar.getInstance();
long referenceDateMilli = c.getTimeInMillis();
String[] selectionArgument = {String.valueOf(referenceDateMilli)};
注意:这仅适用于短信。对于mms来说,
long referenceDateMilli = c.getTimeInMillis()%1000;
String[] selectionArgument = {String.valueOf(referenceDateMilli)};