2013-02-25 54 views
0

后显示在Android通知我有一个PHP ..有人会在我的应用通过的WebView进行登录。 And android application must check www.website.com/notification.php每5秒钟一次。我怎么能检查PHP

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

我想通过Android的检查值0或1 ..

回答

0

您需要AsynchTask执行您的网络代码,专门threadIntentService,这里有一个例子代码:

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活动全部为的时间。

+0

谢谢你的回答。我没有任何关于AsynchTask,专用线程或IntentService的想法,..我如何使用那个代码,我不知道.. – serhat 2013-02-25 05:14:58

+0

我做了一个快速编辑的代码使用专用的'线程'这是三个最简单的。 – iTech 2013-02-25 05:38:43

+0

**请注意,每5秒发出一次网络请求会使电池快速耗尽,因为它会始终保持GSM/3G的激活状态。**但我想知道,whatsapp还能在每秒钟工作吗? – serhat 2013-02-25 05:50:34