如何每5秒请求一次页面而不消耗电池?

3

我正在开发的安卓应用需要每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) {
                  }
              }
          }

        }


  }

缓存和减少请求似乎是显而易见的建议...我真的认为没有更好的解决方法。抱歉。 - Patrick Perini
3个回答

3

需要每5秒检查的原因是什么?最好让服务器通过C2DM向设备发送通知,告知何时有更新可用。如果不断轮询服务器,无论如何实现,都会耗尽电池。


C2dm非常完美!我以前从未知道有这样的工具。非常感谢。它需要经常更新的原因是因为它更像是一个“即时通讯”服务。 - Qasim
如果它更像是即时通讯服务,你不想使用Socket并保持持久连接吗?我认为在这种情况下,套接字可能比C2DM更好用... - nicholas.hauschild
1
这或许值得看一下Android GTalk应用程序的源代码,以了解它是如何处理的:http://android.git.kernel.org/?p=platform/packages/apps/IM.git - Erich Douglass

2
我会考虑使用Android C2DM(来自Android博客)。
基本上,您可以在服务器上注册用户设备,并设置服务器以向用户设备推送通知。然后,您可以更改客户端,使其仅在服务器通知存在新数据时才发出请求。
这应该能很好地运作,因为您将不需要经常发出请求。这样做肯定会节省电池寿命。

0

对于类似聊天的功能,Smack(和Asmack变体)库在Android开发者中非常流行。它使用XMPP提供了一个聊天层。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接