You need to execute your network code in AsynchTask
, dedicated thread
or IntentService
, here is an example code:
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();
Be aware, that making a network request every 5 seconds will drain the battery so quickly, because it will keep the GSM/3G active all the time.