最近在做课程设计,需要写个程序在后台不断向服务器请求数据,因为百度查到IntentService可以用来处理耗时的操作,于是打算使用IntentService来完成。
在mainactivity创建的时候开启IntentService
Intent intent = new Intent(MainActivity.this, ListenService.class);
intent.putExtra(ListenService.EXTRA_MESSAGER, new Messenger(new Myhandler()));
startService(intent);
在自定义的service里面
protected void onHandleIntent(Intent i) {
while(true)
{
//耗时操作
}
}
这样我的service应该在while()里面不断循环,不会结束。但是实际运行的时候,发现只要activity退出之后,service过一段时间也会退出。。。。
有没有大神知道这是怎么回事
http://www.cnblogs.com/zhangs1986/p/3602154.html
IntentService 是可以支持耗时操作的,里面有一个工作子线程和一个消息循环队列的
你先看看这里服务退出是不是其他原因,比如整个进程都被清理掉了