2011-07-30 9 views
0

신청서를 작성하고 있으며 일주일 내에받은/보내지는 현재 SMS 메시지를 웹 서버에 동기화해야합니다. 이것은 내가 가지고있는 코드이지만, 모든 SMS 메시지를 가져옵니다1 주일 이내에 보낸 모든 현재 SMS 메시지를받는 방법

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(); 
    } 

또 다른 문제는 그것이 내가 각 메시지를 하나씩 보낼 경우 많은 처리하는 것입니까? 그룹을 함께 그룹화하여 XML/JSON으로 만들고 인코딩해야합니다 (아마 base64). 그런 다음 내 서버로 보내시겠습니까? 하지만 HTTP POST 매개 변수의 전송 한계를 넘을 수도 있습니다. 문 경우에만 메시지를 제출할를 사용하므로

편집 나는 해결책을 알아 낸, 당신은, 5 열은 모든 메시지의 날짜가 그래서 쿼리, 당신을주는 것을 사용할 필요가 1 주일 이내입니다 (604800 초).

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){ 
         String phoneNumber = c.getString(2); 
        String message = c.getString(11); 
            //this sms is within a week old. 
        } 


      } 

답변

0

는이에 대한 선택 기준을 사용합니다 ..

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

이 올바른 다야를 가져올 수있는 SQL 쿼리를 만드는 것입니다 .. 당신은 선택 기준을 약간 조정할 수도 있습니다 ..

where 키워드를 제외한 sql 쿼리의 선택 조건은 기본적으로 where 절입니다. 여기

이 도움이 SQLLite datetime function

희망에 관한 기사입니다.

+0

receivedDate가 쿼리의 보낸 날짜 날짜일까요? 그것은 실제로 "시간"이라고 불리므로 "날짜 ('now')와 SELECT 사이의 시간을 사용합니다." – Qasim

관련 문제