开发者

How to get all the current SMS messages on the phone that were sent within a week

I am making an application and I need to sync the current sms messages received/sent within a week to my web server. This is the code I have, but it gets all the SMS Messages:

 SharedPreferences settings = getSharedPreferences("MyPrefs", 0);
    boolean firstTime = settings.getBoolean("FirstTime", true);
    if (firstTime) {
        Uri allMessage = Uri.parse("content://sms/");
        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(allMessage, null, null, null, null);
        while  (c.moveToNext()) {
           String row = c.getString(1);
           //upload it all here
        }
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("FirstTime", false);
        editor.commit();
    }

Another question is, will it be a lot to handle if I send each message one by one? Should I like group them together, and make it into XML/JSON and encode it (base64 probably), then send it to my server? But I'm afraid it might go over the sending limit of HTTP POST params.

EDIT I have figured out the solution, you need to use what the query gives you back, so the 5th column is the date for all the messages, so you use an if statement to only submit the messages that are within 1 week (604800 seconds).

Uri allMessage = Uri.parse("content://sms/");
        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(allMessage, null, null, null, null);
            while(c.moveToNext()){
                int date = Math.round(Integer.parseInt(c.getString(4))/1000);
                int time_now = Math.round(System.currentTimeMillis()/1000);
                int difference = time_now - date;
                    if(difference < 604800){
   开发者_Go百科                     String phoneNumber = c.getString(2);
                    String message = c.getString(11);
                                    //this sms is within a week old.
                    }


            }


you need to use the selection criteria for this..

Cursor c = cr.query(allMessage, null, "recievedDate between date('now') and SELECT date('now','start of week','-1 week',''); ", null, null);

this would create a SQL query to fetch the correct daya.. you might want to tweak the selection criteria a bit..

the selection criteria is basically a where clause in an sql query except for the where keyword.

here is an article on SQLLite datetime function

hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜