在我的Android应用程序中,我通过调用以下活动从Activity中启动IntentService
startService(new Intent(this, MyService.class));
它就像一种魅力。我可以在“活动”之间移动,按“主页”按钮以切换到其他应用程序...并且它仍在工作。但是,如果我从“最近”屏幕中删除我的应用程序,则我的服务将停止。如何避免这种情况?换句话说,即使我的应用程序已从最近的应用程序关闭,如何保持我的服务运行?
我的服务代码如下:
public class MyService extends IntentService {
public MyService() {
super("MyService");
}
@Override
protected void onHandleIntent(Intent intent) {
//Here I run a loop which takes some time to finish
}
}
IntentService并非一直运行,但是可以在Intent基础上运行。您使用Intent发送要执行的命令(执行某项工作),服务将执行该工作,并在等待其他Intent之后停止运行。
您应该使用常规服务而不是IntentService来实现您的目标。服务应配置为START_STICKY
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句