如何获取android中特定日期之间发送的SMS消息的数量

时间:2010-11-16 09:31:28

标签: android sms provider

有没有办法检索android中特定日期之间发送的短信数量?

我更喜欢官方支持的SDK功能,这在你的回答中说明这是否是官方SDK的一部分会有所帮助。

我知道this堆栈溢出问题,但似乎使用了不受官方支持的android.provider.Telephony.SMS_RECEIVEDcontent://sms/sent,所以我宁愿不使用它(如果我',请纠正我这是错误的,这是不受支持的。)

1 个答案:

答案 0 :(得分:7)

我知道这是一个非常古老的问题,我刚刚遇到并发现没有答案。因此,请不要因为它而向我投票。也许有人会需要这个答案。

您所要做的就是查询手机内容提供商(我使用CallLog.Calls常量而不是列名称的硬编码字符串)

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

对于您的情况,您所要做的就是提供一个where子句,用于选择投影。您需要以毫秒为单位转换您提到的日期范围(获取日期并将其转换为ms)。确保您的模式与"dd-MM-yy"相对应。

where子句应如下所示:

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

将我们带到查询的最终形式:SELECT date FROM sms_content WHERE date < 'firstbound' AND date < 'second bound'

修改Uri smsContentUri = Uri.parse("content://sms/");,添加到"content://sms/"已发送或收件箱的末尾,以获取"content://sms/sent""content://sms/inbox",了解您要查询的框。 欢呼声。