如何保持每 5 秒请求一次页面而不耗尽电池电量?
我正在开发的 Android 应用程序需要每 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) {
}
}
}
}
}
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) {
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有没有理由需要每 5 秒检查一次?您最好让服务器向设备推送通知(通过 c2dm),告知何时有可用更新。如果您不断轮询服务器,无论您如何实现,您都会耗尽电池。
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.
我会考虑使用 Android C2DM (来自 Android 博客)。
基本上,您可以向服务器注册用户设备,并且可以设置服务器以将通知推送到用户设备。然后,您可以将客户端更改为仅在服务器通知存在新数据时才发出请求。
这应该会很有效,因为您几乎不需要经常发出请求。这样你肯定会节省电池寿命。
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.
对于类似聊天的功能,Smack(和 Asmack 变体)库在 Android 开发人员中非常受欢迎。它使用 XMPP 提供聊天层。
For chat-like functionality, the Smack (and Asmack variant) library is quite popular with Android developers. It provides a chat layer using XMPP.