日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 等5秒钟继续,android – 如何每5秒钟继续请求一个页面而不是杀死电池?

發布時間:2025/3/15 php 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 等5秒钟继续,android – 如何每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 nameValuePairs = new ArrayList(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 all_sms = getBetweenAll(HTML, "", "");

for(int i = 0, size = all_sms.size(); i < size; i++) {

String from = getBetween(all_sms.get(i), "", "");

String to = getBetween(all_sms.get(i), "", "");

String msg = getBetween(all_sms.get(i), "", "");

String sent = getBetween(all_sms.get(i), "", "");

String HTML1 = "";

HttpClient httpclient1 = new DefaultHttpClient();

HttpPost httppost1 = new HttpPost("http://www.***.***/***/add_sms.php");

try {

List nameValuePairs = new ArrayList(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) {

}

}

}

}

}

解決方法:

基本上,您在服務器上注冊用戶設備,并且可以設置服務器以將通知推送到用戶設備.然后,您可以將客戶端更改為僅在服務器通知存在新數據時發出請求.

這應該很好,因為您幾乎不需要經常提出請求.你一定會以這種方式節省電池壽命.

標簽:android,request,http,sync,httpclient

來源: https://codeday.me/bug/20190903/1795612.html

總結

以上是生活随笔為你收集整理的php 等5秒钟继续,android – 如何每5秒钟继续请求一个页面而不是杀死电池?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。