1

I am accessing Android MMS database to get the date of MMS message:

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);

But the date I get is always 1970. Then, I found an answer for this. I need to set the projection to null (to return all columns) and then use the following code to get date value:

//A mystery column of index 2
long timestamp = cursor.getLong(2) * 1000;
//It works !
Date mmsDate = new Date(timestamp);

Everything until here is fine. But, now instead of geting all rows from MMS database, I need to select those rows which were sent after a certain date, which means I need to use selection & selection argument. Something like:

String selection = NAME_OF_MYSTERY_COLUMN_IDX_2 > minDate
Cursor cursor = contentResolver.query(mmsUri, projection, selection, null, null);

But I have no idea what is the name of the column with index 2, how could I achieve what I need ? Is there a workaround?

Community
  • 1
  • 1
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • @Mike M. just run my code sample, you will see, "date" column always returns Thu Jan 01 02:00:00 EET 1970. By the way, my device is Android 4.4.3. As I also have a link in my post, it is not only me have this problem. – Leem.fin Mar 05 '15 at 11:01
  • 1
    @Mike M, yes, you are right, "date" returns correct value after I multiplied it by 1000. Please make an answer, I will accept it. Thanks! – Leem.fin Mar 05 '15 at 11:27

2 Answers2

1

Your first code block is correct, except for the Date instantiation. That constructor expects the time in milliseconds, but the MMS table keeps dates in seconds. To correct this, simply multiply the value returned from the query by 1000.

Date mmsDate = new Date(dateVal * 1000);

For future reference, the Cursor#getColumnName() method will give you the String name for a given column index.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
0

You can try this.

String selection =  "date_sent" > minDate

https://developer.android.com/reference/android/provider/Telephony.BaseMmsColumns.html#DATE_SENT

vishalk
  • 659
  • 1
  • 4
  • 12