Search code examples
androiddatetimesmsandroid-contentresolver

How to access sms time which I have received in inbox?


I am able to read message from the inbox from this:--

 Uri uriSMSURI = Uri.parse("content://sms/inbox");

 Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);

i access date from this:-

   date = cur.getString(cur.getColumnIndexOrThrow("date"));

But now problem is that it gives current time not Message Time from inbox. Sorry for Bad editing & any idea will be appreciated. Thanks in Advance!


Solution

  • Use this code:

    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);
    cursor.moveToFirst();
    String date =  cursor.getString(cursor.getColumnIndex("date"));
    Long timestamp = Long.parseLong(date);    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp);
    Date finaldate = calendar.getTime();
    String smsDate = finaldate.toString();
    Log.d(Home.class.getName(), smsDate);