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

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

问题描述

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

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. 从被杀死到重新启动结束的呼叫顺序是什么?

在一个(相关的)问题上,当服务被操作系统终止(即,没有调用service.onDestroy)时,在服务中启动的正在运行的AsyncTask会发生什么情况?它会继续运行还是随服务一起默默地被撕破?

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中占用内存以强制内存不足?顶级活动应优先使用内存,并迫使服务终止.

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天全站免登陆