在用户退出应用程序后保持后台服务处于活动状态 [英] keeping background service alive after user exit app
问题描述
我正在尝试创建一个服务,即使在用户从正在运行的进程菜单中关闭应用程序(通过将进程移出屏幕)后,它也会为我做后台作业.
I'm trying to create a service that will do background jobs for me even after the user closes the app from the running processes menu(by shifting process out of the screen).
我试图做的是通过如下声明在不同的进程中创建服务:
What I tried to do is create service in a different process by declaring it like this:
<service
android:name=".service.Service"
android:enabled="true"
android:icon="@drawable/ic_launcher"
android:process=":my_process" >
</service>
而 onStartCommand() 是:
and the onStartCommand() is:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
推荐答案
根据 Android 文档您可以通过使用属性来实现此行为:
According to the Android documentation you can achieve this behavior by using the attribute:
android:isolatedProcess="true"
顺便说一句,我知道它没有回答问题,但它也可能对某些人有所帮助 - 最近,我发现了 Evernote 开发人员创建的一个很棒的第三方库.它的名字是 Android-Job,它的目标是创建在不同进程上运行的作业,并且可以成为即使在设备重启后也能再次激活,这真是太棒了.
By the way, I know that it's not answering the question but it might help some people as well - lately, I found out about a great third-party lib that Evernote developers have created. Its name is Android-Job and its aim is to create jobs that run on different processes and can become active again even after a device reboot, it's amazing.
这篇关于在用户退出应用程序后保持后台服务处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!