Android 服务停止 [英] Android service stops

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

问题描述

我离秃顶还有几步之遥...

I've come to the point where I'm a few steps away from going bald...

我正在尝试运行一项服务,该服务将检查我工作中的服务器是否正在运行.应该每 5 分钟执行一次.

I'm trying to run a service that will check if the servers at my work are running. It's supposed to do this every 5 minutes.

我经历了 TimerTask、ScheduledExecutorService 和最后的 Handler.他们都正常"工作了几个小时,除了一些不准确,1-5 分钟,然后突然计时器"停止射击.

I've been through TimerTask, ScheduledExecutorService and finally Handler. They all work "fine" for a few hours, except for some inaccuracy, 1-5 mins off, and then suddenly the "timer" just stops firing.

现在,我读到如果遇到未捕获的异常,调度程序将停止,而且我确信 TimerTask 也是如此 - 但是检查日志文件,根本没有任何异常...

Now, I've read that the Scheduler will stop if it encounters an uncaught Exception, and I'm sure that's the case as well with the TimerTask - But checking the log files, there aren't any exceptions at all...

今天下班回家后,我决定用 Handler 做一个实验.

When I got home from work today I decided to do an experiment with Handler.

我创建了 2 个处理程序,一个将运行用于检查服务器的代码并一次增加一个 int.另一个只会记录所述 int 的值,以便在遇到异常时不会停止(但我看不出它会如何).

I created 2 handlers, one would run the code for checking the servers and increment an int one at a time. The other would just Log the value of said int, so as to not stop should it encountered an exception (I can't see how it would though).

它运行得很好,通常有 1-5 分钟的不准确,几个小时然后停止,下面是日志中的最后几行:

It runs great, with the usual 1-5 mins inaccuracy, for a few hours and then stops, below is the last lines from the log:

02-07 20:03:25.892 D/dalvikvm(  992): GC_EXPLICIT freed 192K, 53% free 4295K/9031K, external 3755K/4825K, paused 114ms
02-07 20:03:35.572 D/dalvikvm( 5472): GC_EXPLICIT freed <1K, 54% free 2895K/6279K, external 2002K/2137K, paused 61ms
02-07 20:04:42.212 V/ServerChecker(12568): Timer triggered
02-07 20:04:42.212 V/ServerChecker(12568): Checking: linux15
02-07 20:04:44.152 V/ServerChecker(12568): Checked: linux15
02-07 20:04:44.152 V/ServerChecker(12568): Checking: linux1
02-07 20:04:44.462 V/ServerChecker(12568): Checked: linux1
02-07 20:04:44.462 V/ServerChecker(12568): Checking: linux12
02-07 20:04:44.762 V/ServerChecker(12568): Checked: linux12
02-07 20:04:44.762 V/ServerChecker(12568): Checking: linux9
02-07 20:04:45.072 V/ServerChecker(12568): Checked: linux9
02-07 20:04:45.072 V/ServerChecker(12568): Checking: linux14
02-07 20:04:45.382 V/ServerChecker(12568): Checked: linux14
02-07 20:04:45.382 V/ServerChecker(12568): Test timer triggered: 13
02-07 20:05:01.002 E/InputDispatcher(  223): channel '406cefc8 com.n04dev.serverchecker/com.n04dev.serverchecker.ServerChecker (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
02-07 20:05:01.002 E/InputDispatcher(  223): channel '406cefc8 com.n04dev.serverchecker/com.n04dev.serverchecker.ServerChecker (server)' ~ Channel is unrecoverably broken and will be disposed!
02-07 20:05:08.932 D/dalvikvm(12842): GC_EXPLICIT freed 73K, 51% free 2641K/5379K, external 2002K/2137K, paused 37ms
02-07 20:05:09.132 D/dalvikvm(  185): GC_EXPLICIT freed 11K, 53% free 2554K/5379K, external 2002K/2137K, paused 96ms
02-07 20:05:12.022 D/dalvikvm(  185): GC_EXPLICIT freed <1K, 53% free 2554K/5379K, external 2002K/2137K, paused 164ms
02-07 20:05:12.062 D/dalvikvm(  185): GC_EXPLICIT freed <1K, 53% free 2554K/5379K, external 2002K/2137K, paused 36ms
02-07 20:05:18.612 D/dalvikvm(12852): GC_EXPLICIT freed 59K, 52% free 2596K/5379K, external 2002K/2137K, paused 72ms

我查看了有关我的应用的最后两条消息,并找到了此线程 - 但我看不出这将如何适用于我的情况.

I've looked up the last two messages concerning my app and found this thread - But I can't see how that would apply in my case.

我真的希望你们对我做错了什么有所了解.我已经用了好几个星期了..尝试不同的计时器,让计时器尝试捕捉主"计时器停止然后重新启动它,但没有运气,正如我上次实验所示,它似乎不是计时器有问题.. 我想..

I really hope you guys have some idea about what I'm doing wrong. I've been at it for weeks.. Trying different timers, having a timer try to catch the "main" timer stopping and then restarting it, but with no luck, and as my last experiment shows, it doesn't seem to be a problem with the timer.. I think..

推荐答案

我经历了 TimerTask、ScheduledExecutorService 和最后的 Handler.他们都正常"工作了几个小时,除了一些不准确,1-5 分钟,然后突然计时器"停止射击.

I've been through TimerTask, ScheduledExecutorService and finally Handler. They all work "fine" for a few hours, except for some inaccuracy, 1-5 mins off, and then suddenly the "timer" just stops firing.

当然.这些都不是为您的目的而设计的,因为既不能使设备保持唤醒状态,也不能很好地与 Android 框架配合使用.在 RAM 中提供 24x7 的服务只是为了标记时间的流逝是一种浪费,Android 在每次发布时都会更加积极地关闭像这样的永久服务.

Of course. None of those are designed for your purpose, as neither keep the device awake, and neither play nicely with the Android framework. Having a service in RAM 24x7 just to mark the passage of time is wasteful, and Android gets more aggressive with each passing release at shutting down everlasting services like this one.

使用AlarmManager 设置您的日程安排和IntentService 来完成实际工作.IntentService 自动为您提供网络 I/O 的后台线程,并在工作完成后自动关闭服务.

Use AlarmManager to set up your schedule and an IntentService to do the actual work. The IntentService automatically gives you your background thread for your network I/O, and it automatically shuts down the service when the work is complete.

如果您打算在设备入睡时发生这种情况,请将 _WAKEUP 警报与 AlarmManager 和我的 WakefulIntentService(或类似的东西).

If you intend to have this occur even if the device falls asleep, use a _WAKEUP alarm with AlarmManager and my WakefulIntentService (or something along those lines).

这篇关于Android 服务停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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