如何强制重启服务? [英] How to force a service restart?

查看:22
本文介绍了如何强制重启服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台服务,当它的内存不足时,它有时会被操作系统杀死.

I have a background service that sometimes gets killed by the OS when it is running low on memory.

  1. 如何模拟这种行为以便我可以调试它?

开发指南只是说如果您的服务已启动,那么您必须将其设计为优雅地处理系统重新启动.如果系统终止您的服务,它会在资源再次可用时立即重新启动它".

The dev guide simply says "if your service is started, then you must design it to gracefully handle restarts by the system. If the system kills your service, it restarts it as soon as resources become available again".

  1. 从它被杀死到完成重新启动的调用顺序是什么?

另一方面(相关的)问题,当服务被操作系统杀死时,在服务中启动的主动运行的 AsyncTask 会发生什么,即没有 service.onDestroy 被调用?它是继续运行还是与服务一起被无声地撕毁?

On a side (related) question, what happens to an actively running AsyncTask started in the service when the service gets killed by the OS, i.e., without service.onDestroy getting called? Does it keep running or get ripped silently along with the service?

推荐答案

在较新的版本下,服务将触发以下事件:

Under newer versions, a service will have the following events triggered:

onCreate()

随后...

int onStartCommand(Intent intent, int flags, int startid)

我知道在上面的评论中你提到了使用它,但值得重复一遍:不要使用旧的onStart()"事件.onStartCommand 是一种新的做事方式.

I know in the comments above you mention using that, but it's worth repeating: Don't use the old "onStart()" event. onStartCommand is the new way of doing things.

onCreate() 可用于创建任何对象等,但在 onStartCommand() 中执行服务的实际代码.

the onCreate() can be used to create any objects, etc. but do the actually code of your service in the onStartCommand().

完成 onStartCommand() 后,您应该返回一个结果.使用START_STICKY"告诉操作系统它可以在需要杀死它时重新启动.使用START_NOT_STICKY"告诉操作系统不要在内存再次可用后尝试重新启动它.这意味着您的应用程序需要再次手动启动该服务.还有其他选项 - 检查 API 文档.

When done with the onStartCommand() you should return a result. Using "START_STICKY" tells the OS it can restart if it needs to kill it. Using "START_NOT_STICKY" tells the os not to bother trying to restart it after memory becomes available again. That means your application would need to manually start the service again. There are other options as well - check the API docs.

检查这些标志可以让您了解服务启动的原因 - 是您自己的应用启动了它,还是操作系统启动了它来重新启动它.您需要定期存储任何重要变量的状态,以便在操作系统重新启动时您可以检索这些变量 - 您可能可以使用 SharedPreferences 私有存储来存储它们.绝对将任何内容存储在 onDestroy 事件中,但不要指望它被调用.

Checking those flags will allow you to see why your service has started - if your own app launched it or if the OS launched it to restart it. You'll need to be periodically storing the state of any important variables so that if the OS has relaunched it you can retrieve those - you could probably use a SharedPreferences private storage to store those. Definitely store any in the onDestroy event, but don't count on that being called.

此外,建议您将 startID 字段存储在一个变量中,并在服务运行完毕后将其与 stopSelfResult(startId) 一起使用.

Also, it's recommended that you store the startID field in a variable and use it with a stopSelfResult(startId) when your service is done running.

请记住,如果您的服务被操作系统终止,您可能没有机会存储任何变量.您需要能够查看您的状态是否是您在重新启动时所期望的状态操作系统,如果不只是重置一切或优雅地死去.

Keep in mind that if your service is killed by the OS you may not have the chance to store any variables. You need to be able to see if your state is where you expect when restarted by the OS and if not just reset everything or gracefully die maybe.

就调试而言,您是否考虑过编写另一个应用程序,该应用程序除了在 Activity 中占用内存之外什么都不做,以强制出现内存不足的情况?top Activity 应该优先于内存并强制服务终止.

As far as debugging, have you considered writing another app that does nothing but suck memory in an Activity in order to force a low memory condition? The top activity should get preference to the memory and force the service to die.

在服务中启动的其他线程仍然是同一个应用程序进程的一部分,因此它们将与服务(以及应用程序的其余部分)一起被杀死.您可以通过添加常规线程来验证这一点在线程中记录语句,然后终止服务.

Additional threads launched in the service are still part of the same application process, so they would be killed along with the service (and the rest of the application.) You can verify this by adding regular log statements inside the threads and then killing the service.

其他可能对您有用的方法是检查您的服务是否已经从您的应用程序内部运行.这是一个函数:

Something else that might be useful for you is checking to see if your service is already running from inside your application. Here's a function to do that:

// Determine if one of my services is currently running
public static boolean isMyServiceRunning(Context context, String servicename) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (servicename.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

这篇关于如何强制重启服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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