How can I keep requesting a page every 5 seconds and not kill the battery?

三世轮回 提交于 2019-12-22 10:53:08

问题


My android app that I am developing needs to request a page on my server every 5 seconds, but Im afraid that will be a big battery consumer, is there any easier possible way? My current approach is a service that loops every 5 seconds:

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) {
                  }
              }
          }

        }


  }

回答1:


I would consider using Android C2DM (from Android blog).

Basically, you register your users device with your server, and you can setup your server to push notifications to your users devices. You can then change the client to only make its requests when the server notifies that there is new data present.

This should work out well, as you will not need to make requests nearly as often. You will definitely save battery life this way.




回答2:


Is there a reason why it needs to check every 5 seconds? You'd be better off having the server push a notification (via c2dm) to the device as to when an update is available. If you're continuously polling the server, you're going to drain the battery no matter how you implement it.




回答3:


For chat-like functionality, the Smack (and Asmack variant) library is quite popular with Android developers. It provides a chat layer using XMPP.



来源:https://stackoverflow.com/questions/6847526/how-can-i-keep-requesting-a-page-every-5-seconds-and-not-kill-the-battery

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