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 が常にアクティブなままになるため、バッテリーが急速に消耗することに注意してください。