我怎能要求每5秒一个页面,而不是杀死电池? [英] How can I keep requesting a page every 5 seconds and not kill the battery?

查看:125
本文介绍了我怎能要求每5秒一个页面,而不是杀死电池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的,我发展需要每5秒钟我的服务器请求一个页面的Andr​​oid应用程序,但我怕这将是一个大的电池的消费者,有没有更简单可行的方法?我目前的做法是,每5秒一个循环服务:

 保护无效onHandleIntent(意向意图){
      而(真){          长ENDTIME = System.currentTimeMillis的()+ 5 * 1000;
          而(System.currentTimeMillis的()<结束时间){
              同步(本){
                  尝试{
                      等待(ENDTIME - System.currentTimeMillis的());
                    HttpClient的HttpClient的=新DefaultHttpClient();
                    HttpPost httppost =新HttpPost(HTTP://www.***.***/***/request_sms.php);
                    串的HTML =;
                    尝试{
                        清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
                        nameValuePairs.add(新BasicNameValuePair(ID,1));
                        httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));                        HTT presponse响应= httpclient.execute(httppost);
                        的HTML = EntityUtils.toString(response.getEntity());
                    }赶上(ClientProtocolException五){}赶上(IOException异常五){}
                        如果(HTML.indexOf([任何文本])大于0){
                        }其他{
                            矢量<串GT; all_sms = getBetweenAll(HTML,< SMS>中,< SMS>);
                            的for(int i = 0,大小= all_sms.size(); I<大小;我++){
                                从= getBetween串(all_sms.get(i)中,&所述;从>中,&所述; /从>中);
                                字符串= getBetween(all_sms.get(i)中,&所述;至>中,&所述; /至>中);
                                串味精= getBetween(all_sms.get(i)中,&所述;味精>中,&所述; /味精>中);
                                串发送= getBetween(all_sms.get(i)中,&所述;发送>中,&所述; /发送>中);
                                字符串HTML1 =;
                                HttpClient的httpclient1 =新DefaultHttpClient();
                                HttpPost httppost1 =新HttpPost(HTTP://www.***.***/***/add_sms.php);
                                尝试{
                                    清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
                                    nameValuePairs.add(新BasicNameValuePair(来自,由));
                                    nameValuePairs.add(新BasicNameValuePair(到,至));
                                    nameValuePairs.add(新BasicNameValuePair(味精,MSG));
                                    nameValuePairs.add(新BasicNameValuePair(已发送,发送));
                                    httppost1.setEntity(新UrlEn codedFormEntity(namevaluepairs中));                                    HTT presponse response1 = httpclient1.execute(httppost1);
                                    HTML1 = EntityUtils.toString(response1.getEntity());
                                    HN.post(新DisplayToast(HTML1));
                                }赶上(ClientProtocolException五){}赶上(IOException异常五){}                            }
                        }
                      }赶上(例外五){
                  }
              }
          }        }
  }


解决方案

我会考虑使用 Android的C2DM (从 Android的博客)。

基本上,你与你的服务器注册用户的设备,你可以设置你的服务器推送通知给用户设备。然后,您可以更改客户端只能使其请求时,服务器通知有新的数据present。

这应该工作顺利,因为你不需要几乎一样经常发出请求。你肯定会节省电池寿命这种方式。

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

        }


  }

解决方案

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.

这篇关于我怎能要求每5秒一个页面,而不是杀死电池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆