即使关闭应用程序,如何保持IntentService的运行? [英] How to keep an IntentService running even when app is closed?

查看:210
本文介绍了即使关闭应用程序,如何保持IntentService的运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我通过调用从Activity中启动IntentService

In my Android app I start an IntentService from within an Activity by calling

startService(new Intent(this, MyService.class));

它就像一种魅力.我可以在活动"之间移动,按主页"按钮以切换到其他应用程序...并且它仍在工作.但是,如果我从最近"屏幕中删除我的应用程序,则我的服务将停止.如何避免这种情况?换句话说,即使我的应用程序已从最近的应用程序中关闭,如何保持我的服务运行?

And it works like a charm. I can move between Activies, press the Home button to switch to other apps... and it's still working. But if I remove my app from the recents screen, my service is stopped. How can I avoid this? In other words, how can I keep my service running even if my app is closed from recent apps?

我的服务代码如下:

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.

IntentService is not running all the time but it works on Intent base. You send, lets say a command to do (do some job), using Intent and service executes that job and stops itself after that waiting for other Intent.
You should use usual Service instead of IntentService to achive your goal. Service should be configured as START_STICKY.

这篇关于即使关闭应用程序,如何保持IntentService的运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆