Android的:如何强制服务(OS杀低内存行为)的重新启动?重新启动、内存、行为、Android

2023-09-12 22:47:44 作者:吹冷风

我有一个后台服务运行内存不足时,有时会被打死的操作系统。还有一些奇怪的行为,这种情况发生的时候,但我不知道(1)如何模拟这种行为,这样我就可以把程序进入调试模式。该开发指南只是说如果您的服务已启动,则必须设计它优雅地处理重新启动的系统,如果系统杀死你的服务,它重新启动它,一旦资源再次可用。什么是呼叫(2)当服务被打死它完成重新启动的顺序?

I have a background service running that sometimes gets killed by the OS when running low on memory. There are some weird behaviors when this happens but I do not know (1) how to simulate this behavior so I can put the process into debug mode. 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". What is (2) the sequence of calls from when the service gets killed to it finishes restarting?

在一个侧面(相关的)问题,会发生什么变化的积极跑动的AsyncTask开始在服务时,该服务被杀害的操作系统,即没有service.onDestroy得到叫什么名字? (3)是否继续运行或得到默默撕开随着服务?

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? (3) Does it keep running or get ripped silently along with the service?

感谢。

推荐答案

在新版本中,服务将具备以下事件触发:

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

onCreate()

其次是...

Followed by...

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()可以用来创建任何对象等,但做你的服务的实际code在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.

检查这些标志将让你明白为什么你的服务已经启动 - 如果你自己的应用程序启动它或者操作系统启动它来重新启动它。你需要定期存储任何重要变量的状态,因此,如果操作系统已经重新启动它,你可以检索这些 - 你很可能使用的是共享preferences私有存储来存储这些。绝对存储任何在的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.

至于调试,你有没有想过写另一个应用程序,什么也不做,但吸内存的活动,以迫使内存不足的情况​​?在顶应该得到preference的内存和强制服务等死。

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;
}