Reading sms received after a date

痴心易碎 提交于 2019-12-20 05:26:13

问题


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.


回答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




回答2:


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

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




回答3:


// 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();
        }


来源:https://stackoverflow.com/questions/9713021/reading-sms-received-after-a-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!