2013-05-27 58 views
1

我想讀取android sms消息的最後14天,但它似乎需要永恆讀出光標的所有消息,所以我限制它的第一個100似乎不按時間順序排列。如何獲得最後14天的android sms

任何有效的查詢數據的想法,以拉動聯繫人和消息只?

我的代碼:

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

回答

3

我建議你使用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"); 
+0

感謝。我試着用下面的日期代碼,但沒有得到任何迴應,我格式化不正確? 日期日期=新日期(System.currentTimeMillis() - 14L * 24 * 3600 * 1000); 結果:週一5月13日22:42:31 GMT + 01:00 2013 – Fearghal

+0

明白了 - 我需要以毫秒計算日期。感謝一個磨坊主。 – Fearghal

相關問題