[英]service to run http command causing infinite loop
基本上,我想让服务运行并在后台生成http请求(因此执行Async任务)。 当连接出现错误时,我希望服务停止。 这将仅在LAN上运行,并在从主机提示使用新的IP地址时停止并重新启动,并在用户退出应用程序时停止。
我可以使服务启动,停止并更新ip。 当IP地址不再可用时,麻烦就来了。 我得到了错误,我抓住了它们。 我破坏了服务,它一直在继续……像僵尸一样……四肢坚不可摧,渴望饥饿
这是服务:
public class fanInfoService extends Service {
public String _ip = null;
public String _stopService = null;
public Long _startTime = (long) 0;
public fanInfoService() {
super();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// get information from intent
Bundle b = intent.getExtras();
_ip = b.getString("ip");
_stopService = b.getString("STOP");
_startTime = b.getLong("startTime");
if(_stopService != null){
alarmCancel();
onDestroy();
System.out.println("stopping due to stopService");
}else{
//else set stopService to null and run as usual
//_stopService = null;
System.out.println("onhandle called again with " + _ip);
try{
new RequestTask().execute("http://"+ _ip +"/fanspd.cgi");
}catch(RuntimeException e){
System.out.println("we caught an error");
}
// RequestTask("http://"+ _ip +"/fanspd.cgi");
}
return START_NOT_STICKY;
}
@Override
public void onDestroy(){
//if we want to stop service come here
super.onDestroy();
//super.stopSelf();
System.out.println("Here we are stopping things on Destroy");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private void scheduleNextUpdate()
{
Intent intent = new Intent(this, this.getClass());
Bundle b = new Bundle();
b.putString("ip", _ip);
b.putLong("startTime", _startTime);
intent.putExtras(b);
PendingIntent pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// The update frequency should often be user configurable. This is not.
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 2500;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//Also stop if the signal comes to stop
if(_stopService != null){
if(_stopService.equals("stop")){
System.out.println("here I am Stopping the Refresh Thread " + _stopService);
alarmCancel();
onDestroy();
}else{
System.out.println("Somthings wrong in fanService _stopService");
}
}else{
//alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
System.out.println("Here we set the repeat call");
alarmManager.setInexactRepeating(AlarmManager.RTC, nextUpdateTimeMillis, 30, pendingIntent);
}
}
//Here is the async class to send http request
public class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
System.out.println("Client Side Error");
e.printStackTrace();
runErrorCode();
return null;
} catch (IOException e) {
System.out.println("I-O side Error");
e.printStackTrace();
runErrorCode();
onDestroy();
return null;
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
//Take response and send back to Main for parseHTTP via intent
Intent intent = new Intent("com.example.airscapefancontroller.android.action.broadcast");
//System.out.println("Here is results " + result);
Bundle extras = new Bundle();
extras.putString("RESULT", result);
extras.putString("REFRESH", null);
intent.putExtras(extras);
sendBroadcast(intent);
scheduleNextUpdate();
onDestroy();
System.out.println("I just posted results");
}
}
private void runErrorCode(){
Intent intent = new Intent("com.example.airscapefancontroller.android.action.broadcast");
//send a signal back to reset the stats
Bundle extras = new Bundle();
extras.putString("RESULT", "refresh");
intent.putExtras(extras);
System.out.println("Here we are in error code ");
// alarmCancel();
sendBroadcast(intent);
stopSelf();
onDestroy();
}
private void alarmCancel(){
Intent intent = new Intent(this, this.getClass());
Bundle b = new Bundle();
b.putString("ip", _ip);
b.putLong("startTime", _startTime);
intent.putExtras(b);
PendingIntent pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
onDestroy();
System.out.println("We canceled the service and alarm");
}
这是设备离开网络的LogCat:
07-09 18:27:56.195: I/System.out(13375): we caught an error
07-09 18:27:56.250: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.250: I/System.out(13375): we caught an error
07-09 18:27:56.257: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.261: I/System.out(13375): we caught an error
07-09 18:27:56.281: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.281: I/System.out(13375): we caught an error
07-09 18:27:56.312: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.312: I/System.out(13375): we caught an error
07-09 18:27:56.343: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.343: I/System.out(13375): we caught an error
07-09 18:27:56.378: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.378: I/System.out(13375): we caught an error
07-09 18:27:56.402: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.402: I/System.out(13375): we caught an error
07-09 18:27:56.414: I/System.out(13375): I-O side Error
07-09 18:27:56.414: W/System.err(13375): org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.0.29:80 timed out
07-09 18:27:56.414: W/System.err(13375): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
07-09 18:27:56.414: W/System.err(13375): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
07-0 9 18:27:56.414: W/System.err(13375): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-09 18:27:56.414: W/System.err(13375): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-09 18:27:56.414: W/System.err(13375): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
07-09 18:27:56.417: I/System.out(13375): I-O side Error
07-09 18:27:56.417: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-09 18:27:56.417: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-09 18:27:56.417: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-09 18:27:56.417: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:137)
07-09 18:27:56.417: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:1)
07-09 18:27:56.417: W/System.err(13375): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-09 18:27:56.417: W/System.err(13375): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-09 18:27:56.417: W/System.err(13375): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-09 18:27:56.417: W/System.err(13375): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-09 18:27:56.417: W/System.err(13375): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-09 18:27:56.421: W/System.err(13375): at java.lang.Thread.run(Thread.java:1019)
07-09 18:27:56.421: I/System.out(13375): Here we are in error code
07-09 18:27:56.437: W/System.err(13375): org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.0.29:80 timed out
07-09 18:27:56.441: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.441: I/System.out(13375): we caught an error
07-09 18:27:56.449: W/System.err(13375): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
07-09 18:27:56.457: I/System.out(13375): I-O side Error
07-09 18:27:56.460: W/System.err(13375): org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.0.29:80 timed out
07-09 18:27:56.460: I/System.out(13375): onhandle called again with 10.0.0.29
07-09 18:27:56.460: I/System.out(13375): we caught an error
07-09 18:27:56.464: I/System.out(13375): Here we are stopping things on Destroy
07-09 18:27:56.468: I/System.out(13375): Here we are stopping things on Destroy
07-09 18:27:56.468: I/System.out(13375): Here we are stopping things on Destroy
07-09 18:27:56.472: I/System.out(13375): Here we set the repeat call
07-09 18:27:56.472: I/System.out(13375): Here we are stopping things on Destroy
07-09 18:27:56.472: I/System.out(13375): I just posted results
07-09 18:27:56.472: W/System.err(13375): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
07-09 18:27:56.472: W/System.err(13375): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
07-09 18:27:56.476: W/System.err(13375): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-09 18:27:56.480: I/System.out(13375): I-O side Error
07-09 18:27:56.480: W/System.err(13375): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-09 18:27:56.484: W/System.err(13375): org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.0.29:80 timed out
07-09 18:27:56.484: W/System.err(13375): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
07-09 18:27:56.484: W/System.err(13375): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
07-09 18:27:56.484: W/System.err(13375): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
07-09 18:27:56.488: W/System.err(13375): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-09 18:27:56.488: W/System.err(13375): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-09 18:27:56.488: W/System.err(13375): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
07-09 18:27:56.488: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-09 18:27:56.488: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-09 18:27:56.488: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-09 18:27:56.488: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:137)
07-09 18:27:56.488: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:1)
07-09 18:27:56.492: W/System.err(13375): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-09 18:27:56.496: W/System.err(13375): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
07-09 18:27:56.496: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-09 18:27:56.496: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-09 18:27:56.496: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-09 18:27:56.496: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:137)
07-09 18:27:56.500: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:1)
07-09 18:27:56.500: W/System.err(13375): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-09 18:27:56.503: W/System.err(13375): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-09 18:27:56.507: W/System.err(13375): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-09 18:27:56.511: I/System.out(13375): I-O side Error
07-09 18:27:56.519: W/System.err(13375): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-09 18:27:56.523: W/System.err(13375): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-09 18:27:56.523: W/System.err(13375): at java.lang.Thread.run(Thread.java:1019)
07-09 18:27:56.523: I/System.out(13375): Here we are in error code
07-09 18:27:56.523: W/System.err(13375): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-09 18:27:56.527: W/System.err(13375): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-09 18:27:56.527: W/System.err(13375): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
07-09 18:27:56.527: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-09 18:27:56.527: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-09 18:27:56.527: W/System.err(13375): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-09 18:27:56.527: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:137)
07-09 18:27:56.527: W/System.err(13375): at com.example.airscapefancontroller.fanInfoService$RequestTask.doInBackground(fanInfoService.java:1)
07-09 18:27:56.531: W/System.err(13375): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-09 18:27:56.531: W/System.err(13375): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-09 18:27:56.550: W/System.err(13375): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
感谢凯飞的帮助。 我为您解决有关此问题的编码器问题提供了答案。
万一有人有类似的问题,这篇文章对我很有帮助。
lq = 1 有用的条目
我基本上将整个服务改回了IntentService,并实例化了一个全局公共静态volatile布尔值并将其设置为shouldContinue = true;。
布尔值指示召回的方式和时间。 完美运作
服务应通过调用stopSelf()方法来终止自身。 onDestroy()由Android OS调用。
参考: http : //www.vogella.com/tutorials/AndroidServices/article.html#service_stop
万一有人有类似的问题,这篇文章对我很有帮助。
我基本上将整个服务改回了IntentService,并实例化了一个全局公共静态volatile布尔值并将其设置为shouldContinue = true;。
布尔值指示召回的方式和时间。 完美运作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.