Android-Service 被“不再想要"杀死- 如何重新启动它? [英] Android-Service killed with "no longer want" - how to restart it?

查看:46
本文介绍了Android-Service 被“不再想要"杀死- 如何重新启动它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一项服务,可以持续收集手机上的传感器数据.该服务应该永远"运行,例如只要用户愿意,不会被系统杀死.

We have a Service that continuously collects sensor data on the phone. This service should run "forever", e.g. as long as the user wants, and not be killed by the system.

澄清一下,此服务旨在用于向公众发布市场的应用程序,它是为科学研究而编写的.所以运行该应用程序的人完全意识到他们的电池会比平时更快地耗尽,这没问题.

For clarification, this service is not intended for an app to be released in the market to the general public, it is written for a scientific study. So the people running the app are fully aware that their battery will sucked empty faster than usual, this is no problem.

无论如何,我的问题是服务在运行一段时间后被终止.有时在一个小时后,有时仅在 7 小时或 10 小时后.

Anyways, my problem is that the service gets killed after a while of running. Sometimes after an hour, sometimes only after like 7 oder 10 hours.

服务被终止时的日志条目如下所示.据我所知,它只是说不再需要",有时甚至没有调用 onDestroy().

The Log-Entries when the service gets killed look like this. It just says "no longer want", sometimes without even calling onDestroy(), as far as I can tell.

07-20 17:07:11.593 I/ActivityManager(   85): No longer want my.project.datalogging (pid 23918): hidden #16
07-20 17:07:11.593 I/WindowManager(   85): WIN DEATH: Window{44c61570 my.project.datalogging/my.project.datalogging.DataLoggingApp paused=false}
07-20 17:07:11.603 I/BackgroundService(23925): onDestroy()

或稍后(在我手动重新启动它之后):

Or later (after I manually restarted it):

07-20 19:00:49.677 I/ActivityManager(   85): No longer want my.project.datalogging:BackgroundService (pid 24421): hidden #17
07-20 19:00:49.677 I/ActivityManager(   85): No longer want my.project.datalogging (pid 24415): hidden #18
07-20 19:00:49.807    85 10707 I WindowManager: WIN DEATH: Window{44f1ea58 my.project.datalogging/my.project.datalogging.DataLoggingApp paused=false}

我经常看到其他服务因不再需要"而被杀死,然后立即以计划重新启动崩溃的服务"重新启动.例如这里使用 runkeeper:

I often see other services being killed with "no longer want" and then immediately restarted with "Scheduling restart of crashed service". For example here with runkeeper:

07-20 17:30:45.503 I/ActivityManager(   85): No longer want com.fitnesskeeper.runkeeper (pid 24090): hidden #16
07-20 17:30:45.603 W/ActivityManager(   85): Scheduling restart of crashed service com.fitnesskeeper.runkeeper/.services.RunKeeperService in 5000ms
07-20 17:33:52.989 I/ActivityManager(   85): Start proc com.fitnesskeeper.runkeeper for service com.fitnesskeeper.runkeeper/.services.RunKeeperService: pid=24292 uid=10099 gids={3003, 1015}

日志没有显示内存不足的记录.测试在(多台)Nexus One 上完成,版本为 2.2 (Froyo FRF91).

The logs show no record of low memory. Testing is done on (several) Nexus One with 2.2 (Froyo FRF91).

有什么办法可以通过我的应用实现这种行为吗?被杀后自动重启?

Is there any way I can achieve this behaviour with my app? Automatic restarting after being killed?

或者这是完全不同的东西,只是在 logcat 中看起来相似?

Or this this something totally different that just looks similar in logcat?

如果您需要更多信息,请询问,我会尽力提供:-)

If you need any more information, just ask and I'll try to provide it :-)

推荐答案

如何启动服务?你试过 startForeground()?当然,这不是绝对的保证,但至少应该延长使用寿命.

How do you start the service? Have you tried startForeground()? That's not an absolute guarantee, of course, but should at least lengthen the lifespan.

这篇关于Android-Service 被“不再想要"杀死- 如何重新启动它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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