保持服务运行 [英] Keep Service running
问题描述
有人可以告诉我在用户关闭服务时使其始终运行或重新启动的方法吗?我已经看到清除内存后,facebook服务会重新启动. 我不想做ForegroundServices.
Can anyone tell me the way to keep a Service always running or restarting itself when the user close it? I've watched that facebook services restart when i clear memory. I don't want to make ForegroundServices.
推荐答案
您应该创建一个粘性服务.在此处了解详情.
You should create a sticky service. Read more about it here.
您可以通过在onStartCommand中返回START_STICKY来完成此操作.
You can do this by returning START_STICKY in onStartCommand.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
还请阅读有关 application:persistent 的信息. 应用程序是否应始终保持运行".这更麻烦-系统将尝试不杀死您的应用程序,这会影响系统中的其他人,您应该小心使用它.
Read also about application:persistent which is "Whether or not the application should remain running at all times". This is more troublesome - System will try not to kill your app which will effect others in the system, you should be careful using it.
这篇关于保持服务运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!