我正在访问Android数据库以获取MMS消息的日期:
Uri mmsUri = Uri.parse("content://mms/");
ContentResolver contentResolver = getContentResolver();
String[] projection = {"_id", "date"};
Cursor cursor = contentResolver.query(mmsUri, projection, null, null, null);
long dateVal = cursor.getLong(cursor.getColumnIndex("date"));
//This date is always 1970
Date mmsDate = new Date(dateVal);但我得到的日期总是1970年。然后,我为这个找到了一个answer。我需要将projection设置为null (返回所有列),然后使用以下代码获取日期值:
//A mystery column of index 2
long timestamp = cursor.getLong(2) * 1000;
//It works !
Date mmsDate = new Date(timestamp);这里之前的一切都很好。但是,现在我不需要从MMS数据库中获取所有行,而是需要选择那些在某个日期之后发送的行,这意味着我需要使用Selection&Selection参数。类似于:
String selection = NAME_OF_MYSTERY_COLUMN_IDX_2 > minDate
Cursor cursor = contentResolver.query(mmsUri, projection, selection, null, null);但是我不知道索引2列的名称是什么,我如何才能实现我所需要的?有解决办法吗?
发布于 2015-03-05 11:28:12
除了Date实例化之外,第一个代码块是正确的。该构造函数期望时间以毫秒为单位,但MMS表以秒为单位保存日期。要纠正这一点,只需将查询返回的值乘以1000。
Date mmsDate = new Date(dateVal * 1000);作为以后的参考,Cursor#getColumnName()方法将为给定的列索引提供字符串名称。
发布于 2015-03-05 10:39:55
https://stackoverflow.com/questions/28875597
复制相似问题