Skip to content Skip to sidebar Skip to footer

Reading Sms Received After A Date

I'm trying to read all the sms I received after a date. Here is the code: Uri SMS_CONTENT_URI = Uri.parse('content://sms'); Uri SMS_INBOX_CONTENT_URI = Uri.withAppendedPath(SMS_C

Solution 1:

I use following code to retrieve sms messages after a particular date.

// First select the date shown by the datepicker.
// Here I am assuming that a DatePicker object is already created with id dpResult
// to select a particular date.
DatePicker datePicker = (DatePicker) findViewById(R.id.dpResult);


// Now create a SimpleDateFormat object.        
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

// Add 1 in month as its 0 based indexing in datePicker but not in SimpleDateFormat
String selectedDate = datePicker.getYear() + "-" +  (datePicker.getMonth() + 1) + "-" +    datePicker.getDayOfMonth();

// Now create a start time for this date in order to setup the filter.
Date dateStart = formatter.parse(selectedDate + "T00:00:00");

// Now create the filter and query the messages.
String filter = "date>=" + dateStart.getTime();
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(SMS_INBOX, null, filter, null, null);

while(cursor.moveToNext()) {

    // Perform the required stuff here.             
}
cursor.close();

Got above code snippet from http://realembed.blogspot.com/2013/11/retrieve-sms-message-on-particular-date.html


Solution 2:

// Just pass time stamp as a third parameter in your cursor query . and you will get filtered date in ascending order. refer below.

     Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"),
 new String[] { "address", "date", "body", },"date>=1535547019783",null,"date ASC");

            if (cursor.moveToFirst()) { // must check the result to prevent exception
                do {
                    String msgData = "";
                    for(int ati=0;ati<cursor.getColumnCount();ati++)

                    {
                        msgData = "" + cursor.getColumnName(ati) + ":" + cursor.getString(ati);
                       String date= cursor.getString(cursor.getColumnIndex("body"));
                        Log.e("DATA",date);
                    }

                    // use msgData
                } while (cursor.moveToNext());
            } else {
                // empty box, no SMS
            }
    cursor.close();
        }

Solution 3:

Did you try "date>='61291393200000'"?

It seems like numeric value SQL statement needs a quote 'xxx'.


Post a Comment for "Reading Sms Received After A Date"