how to get last 14 days of android sms

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

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.

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

My code:

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

回答1:

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 

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");  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!