Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading sms received after a date

Tags:

android

uri

sms

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_CONTENT_URI, "inbox");

Cursor cursor = context.getContentResolver().query( SMS_INBOX_CONTENT_URI, new String[] { "_id" }, "date>=61291393200000", null, null);  
//61291393200000 = 03/01/12

This returns me a empty cursor.

When I was executing this code:

Cursor cursor = context.getContentResolver().query( SMS_INBOX_CONTENT_URI, new String[] { "_id" }, "read=1", null, null);

Was returning me all the sms.

Somebody knows how to filter the sms by date?

I tried to execute in the send sms too, but had the same problem.

like image 482
jonathanrz Avatar asked Dec 12 '25 01:12

jonathanrz


2 Answers

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

like image 107
Arslan Avatar answered Dec 14 '25 14:12

Arslan


// 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();
        }
like image 24
atish naik Avatar answered Dec 14 '25 15:12

atish naik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!