2011-07-27 25 views
3

我正在開發的我的Android應用程序需要每5秒鐘在我的服務器上請求一個頁面,但是我擔心這會是一個龐大的電池消費者,有沒有更簡單的方法?我目前的做法是每5秒循環一次的服務:我如何每5秒鐘請求一次頁面而不殺死電池?

protected void onHandleIntent(Intent intent) { 
     while (true){ 

      long endTime = System.currentTimeMillis() + 5*1000; 
      while (System.currentTimeMillis() < endTime) { 
       synchronized (this) { 
        try { 
         wait(endTime - System.currentTimeMillis()); 


        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("http://www.***.***/***/request_sms.php"); 
        String HTML = ""; 
        try { 
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
         nameValuePairs.add(new BasicNameValuePair("id", "1")); 
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

         HttpResponse response = httpclient.execute(httppost); 
         HTML = EntityUtils.toString(response.getEntity()); 
        } catch (ClientProtocolException e) {} catch (IOException e) {} 


         if(HTML.indexOf("[NO TEXTS]") > 0) { 
         } else { 
          Vector<String> all_sms = getBetweenAll(HTML, "<sms>", "<sms>"); 
          for(int i = 0, size = all_sms.size(); i < size; i++) { 
           String from = getBetween(all_sms.get(i), "<from>", "</from>"); 
           String to = getBetween(all_sms.get(i), "<to>", "</to>"); 
           String msg = getBetween(all_sms.get(i), "<msg>", "</msg>"); 
           String sent = getBetween(all_sms.get(i), "<sent>", "</sent>"); 
           String HTML1 = ""; 
           HttpClient httpclient1 = new DefaultHttpClient(); 
           HttpPost httppost1 = new HttpPost("http://www.***.***/***/add_sms.php"); 
           try { 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
            nameValuePairs.add(new BasicNameValuePair("from", from)); 
            nameValuePairs.add(new BasicNameValuePair("to", to)); 
            nameValuePairs.add(new BasicNameValuePair("msg", msg)); 
            nameValuePairs.add(new BasicNameValuePair("sent", sent)); 
            httppost1.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

            HttpResponse response1 = httpclient1.execute(httppost1); 
            HTML1 = EntityUtils.toString(response1.getEntity()); 
            HN.post(new DisplayToast(HTML1)); 
           } catch (ClientProtocolException e) {} catch (IOException e) {} 

          } 
         } 


         } catch (Exception e) { 
        } 
       } 
      } 

     } 


    } 
+0

緩存和請求較少似乎是一個明顯的建議......不認爲有更好的解決方法。抱歉。 –

回答

2

我會考慮使用Android C2DM(來自Android blog)。

基本上,您註冊您的用戶設備與您的服務器,並且您可以設置您的服務器推送通知到您的用戶設備。然後,您可以將客戶端更改爲僅在服務器通知存在新數據時發出請求。

這應該工作得很好,因爲您不需要經常提出請求。你一定會以這種方式節省電池壽命。

3

是否有一個原因,爲什麼它需要檢查每隔5秒?您最好讓服務器通過(通過c2dm)向設備推送更新時的通知。如果您不斷地輪詢服務器,無論您如何實施它,都會耗盡電池電量。

+0

C2dm非常完美!我從來沒有新的有這樣的工具。非常感謝。其更新頻率非常高的原因是它更像是「即時通訊」服務。 – Qasim

+0

如果它將是更多的即時消息服務,你不願意使用'Socket'並保持持久連接嗎?我認爲套接字在這種情況下可能比C2DM更好... –

+1

可能值得看看Android GTalk應用程序的來源,看看它在那裏的處理方式:http://android.git.kernel.org/? p =平臺/產品/應用/ IM.git –

0

對於類似於聊天的功能,Smack(和Asmack變體)庫在Android開發人員中非常流行。它使用XMPP提供聊天圖層。