如何获得最近14天的Android短信 [英] how to get last 14 days of android sms

查看:229
本文介绍了如何获得最近14天的Android短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读Android sms消息的最后14天,但似乎需要花费一个永恒时间才能读出Cursor的所有消息,所以我将它限制在第1个100,这似乎不是按时间顺序排列的。

I am trying to read the last 14 days of android sms messages however it seems to take an eternity to read out all the messages from the Cursor so i limit it to the 1st 100 which dont seem to be in chronological order.

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

Any ideas of an efficient query of th esms data in order to pull the contact and the message only?

我的代码:

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++


推荐答案

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

I suggest you use the ContentResolver query to get only the records that interest you. you can select different columns, specify where clauses and even sort..

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子句查询它:

Now you just need to query it with a good where clause:

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

这篇关于如何获得最近14天的Android短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆