Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 632
Deepanker Chaudhary Avatar asked Oct 30 '12 11:10

Deepanker Chaudhary


3 Answers

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);
like image 176
Sunil Mishra Avatar answered Oct 12 '22 08:10

Sunil Mishra


In Sms tables, Dates are stored as INTEGER in milliseconds. so, use

millis = cur.getLong(cur.getColumnIndexOrThrow("date")) on Cursor.

Then, use

DateFormat.format("EEEE, MMMM dd, yyyy h:mm:ss aa", new Date(millis))

to get a nice readable string.

like image 27
S.D. Avatar answered Oct 12 '22 08:10

S.D.


    /**
 * Read messages of respective phone number from msg 
 * 
 * @param activity
 * @param ph_number
 * @return
 */

public static void getMessageDetails(Activity activity, String ph_number) {


    String forDelete_thread_id = "";
    String forDelete_id = "";
    String address = ph_number;
    Uri allMsg = Uri.parse("content://sms/");
    Cursor managedCursor = activity.getContentResolver().query(
            allMsg,
            new String[] { "_id", "thread_id", "address", "date", "body",
                    "type" }, "address=?", new String[] { address }, null);

    while (managedCursor.moveToNext()) {

        String msg_id = managedCursor.getString(0);
        String thread_id = managedCursor.getString(1);
        String address_b = managedCursor.getString(2);
        String date = managedCursor.getString(3);
        String msg_body = managedCursor.getString(4);
        String type = managedCursor.getString(5);


    }
}
like image 40
taran mahal Avatar answered Oct 12 '22 07:10

taran mahal