0

Is there a way to retrieve the number of sent SMS messages between specific dates in android?

I would prefer an officially supported SDK functionality, this stating in your answer whether this is part of the official SDK would be helpful.

I am aware of this stack overflow question but it seems to use the not officially supported android.provider.Telephony.SMS_RECEIVED and content://sms/sent, so I would rather not use it (please correct me if i'm wrong about this being unsupported).

Community
  • 1
  • 1
rony l
  • 5,798
  • 5
  • 35
  • 56

1 Answers1

7

I know it is very old question, I just came across and spotted as unanswered. So please do not downvote me just because of it. Maybe someone will need this answer.

All you have to do is to query the phone content provider (I used CallLog.Calls constants instead hardcoded strings for columns names)

 String[] projection = { CallLog.Calls.DATE };
 Uri smsContentUri = Uri.parse("content://sms/");
 Cursor cursor = this.getContentResolver().query(smsContentUri , selection,where, null, null);
 SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
 cursor.moveToFirst();
  Log.d("SMS COUNT", ""+cursor.getCount()); 
  while(cursor.isAfterLast() == false){
        Log.d("SMS DATE", ""+cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE))); //raw format of the date in milliseconds
        long callDate = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));      
        String dateString = format.format(new Date(callDate));
        Log.i("SMS DATE",dateString);
        cursor.moveToNext();
 }

For your case all you have to do is to supply a where clause which will make selection over the projection. You need to convert bounds of dates you've mentioned in miliseconds (get the date and convert it to ms). Make sure your pattern corresponds to "dd-MM-yy".

The where clause should look like:

String where = CallLog.Calls.DATE+"<"+FRIST_BOUND_IN_MS + " AND "+ CallLog.Calls.DATE + "< " + SECOND_BOUND_IN_MS;

which take us to the final form of our query: SELECT date FROM sms_content WHERE date < 'firstbound' AND date < 'second bound'

Modify Uri smsContentUri = Uri.parse("content://sms/"); adding to the end of the "content://sms/" sent or inbox to get "content://sms/sent" or "content://sms/inbox", regarding which box you want to query. Cheers.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • i'm upvoting but not selecting because this answer does not really solve the major issue I had about content://sms being an unofficial API. – rony l Oct 16 '11 at 12:48
  • I don't think there is an official API for this. This should work on most devices. – midhunhk Nov 19 '16 at 18:07