0

我有一个php .. 有人将通过webview登录我的应用程序。android 应用程序必须每 5 秒检查一次www.website.com/notification.php 。

 if notification php ==> echo 1 ; .. show notification.
 if notification php ==> echo 0 ; .. dont do anything..

我想通过 android 检查 0 或 1 值..

4

1 回答 1

0

您需要在AsynchTask、专用thread或中执行您的网络代码IntentService,这是一个示例代码:

Thread networkThread = new Thread() {
   public void run() {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://www.website.com/notification.php");
        HttpResponse response = httpclient.execute(httppost);
        String result = EntityUtils.toString(httpresponse.getEntity());

        if("1".equals(result.trim()){
            showNotification(); // You have to implement this
        }
    }
}
networkThread.start();

请注意,每 5 秒发出一次网络请求会很快耗尽电池电量,因为它会一直保持 GSM/3G 处于活动状态

于 2013-02-25T03:29:41.000 回答