【问题标题】:how to get last 14 days of android sms如何获取最近 14 天的安卓短信
【发布时间】:2013-05-28 06:29:03
【问题描述】:

我正在尝试阅读过去 14 天的 android 短信消息,但是似乎需要很长时间才能从光标中读出所有消息,因此我将其限制为第 100 条似乎不按时间顺序排列的消息。

关于有效查询 esms 数据以便仅提取联系人和消息的任何想法?

我的代码:

Uri uriSMSURISent = Uri.parse("content://sms/sent"); // get the sms data for sent
Cursor curSent = getContentResolver().query(uriSMSURISent, null, null, null,null);

    int i=0;
   while (curSent.moveToNext() && i<100) 
    {
            String from = curSent.getString(2);
            if(sentHashmap.containsKey(to))
            {
                String cumulativeMessage = sentHashmap.get(to); 
                sentHashmap.put(from, cumulativeMessage+ " " +curSent.getString(12));
            }
            else
                sentHashmap.put(from, curSent.getString(12));
i++

【问题讨论】:

    标签: java android sms


    【解决方案1】:

    我建议您使用 ContentResolver 查询仅获取您感兴趣的记录。 您可以选择不同的列,指定 where 子句甚至排序..

    http://developer.android.com/guide/topics/providers/content-provider-basics.html

    // Queries the user dictionary and returns results 
    mCursor = getContentResolver().query(
         UserDictionary.Words.CONTENT_URI,   // The content URI of the words table
         mProjection,                        // The columns to return for each row
         mSelectionClause                    // Selection criteria
         mSelectionArgs,                     // Selection criteria
         mSortOrder); // The sort order for the returned rows Table 2  shows how the arguments to
    
    
     query(Uri,projection,selection,selectionArgs,sortOrder) match an SQL
     SELECT statement:
    
    
    // column names for above provider:
    0: _id
    1: thread_id
    2: address
    3: person
    4: date
    5: protocol
    6: read   
    7: status
    8: type
    9: reply_path_present
    10: subject
    11: body
    12: service_center
    13: locked
    

    现在你只需要用一个好的 where 子句来查询它:

      long date = new Date(System.currentTimeMillis() - 14L * 24 * 3600 * 1000).getTime();
      Cursor curSent = getContentResolver().query(uriSMSURISent, null,"date" + ">?",new String[]{""+date},"date DESC"); 
    

    【讨论】:

    • 谢谢。我用下面的日期代码试过了,但什么也没回来,我是不是格式化不正确?日期 date = new Date(System.currentTimeMillis() - 14L * 24 * 3600 * 1000);结果:2013 年 5 月 13 日星期一 22:42:31 GMT+01:00
    • 知道了 - 我需要以毫秒为单位获取日期。谢谢磨坊人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2019-09-14
    • 1970-01-01
    相关资源
    最近更新 更多