谁能告诉我要保持服务始终运行或重新启动本身,当用户关闭它的方法是什么?我看过了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.
推荐答案
您应该创建一个棘手的服务。了解更多关于它here.
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.
相关推荐
最新文章