安卓:保持后台服务活(preventing过程中死亡) [英] Android: keeping a background service alive (preventing process death)

查看:171
本文介绍了安卓:保持后台服务活(preventing过程中死亡)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被定义为一个服务:

I have a service that is defined as:

public class SleepAccelerometerService extends Service implements SensorEventListener

从本质上讲,我想提出一个应用程序,监视各种原因加速度计活动,而在用户与睡在床上他或她的手机/设备。这是一个长期运行的服务,绝不能在夜间被杀害。根据在夜间很多后台应用程序和周期过程是如何发生的,机器人有时会杀死我的过程,从而结束我的服务。例如:

Essentially, I am making an app that monitors accelerometer activity for various reasons while the user sleeps with his or her phone/device on the bed. This is a long-running service that MUST NOT be killed during the night. Depending on how many background apps and periodic processes occur during the night, android sometimes kills off my process, thereby ending my service. Example:

10-04 03:27:41.673: INFO/ActivityManager(1269): Process com.androsz.electricsleep (pid 16223) has died.
10-04 03:27:41.681: INFO/WindowManager(1269): WIN DEATH: Window{45509f98 com.androsz.electricsleep/com.androsz.electricsleep.ui.SleepActivity paused=false}

我不希望强迫用户有'SleepActivity或在我的应用程序作为前景的一些其他活动。我不能让我的服务定期运行,因为它在不断地拦截onSensorChanged。

I do not want to force the user to have 'SleepActivity' or some other activity in my app as the foreground. I can't have my service run periodically, because it is constantly intercepting onSensorChanged.

任何提示?来源$ C ​​$ c是在这里: HTTP://$c$c.google.com/p/electricsleep/

Any tips? source code is here: http://code.google.com/p/electricsleep/

推荐答案

有关的Andr​​oid 2.0或更高版本,你可以使用<一个href="http://developer.android.com/intl/de/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29"><$c$c>startForeground()法在前台启动服务。

For Android 2.0 or later you can use the startForeground() method to start your Service in the foreground.

的<一个href="http://developer.android.com/intl/de/reference/android/app/Service.html#ProcessLifecycle">documentation说下面:

一个启动的服务可以使用 startForeground(INT,通知) API把服务前台状态,其中系统认为它是一些用户积极意识到并因此不能对内存查杀时低的候选人。 (它仍然是理论上可能的服务可以在极端的存储器pressure从当前前台应用程序死亡,但在实践中,这不应该是一个关注的问题。)

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

的主要用于当杀服务将是破坏性的用户使用,例如杀死一个音乐播放器服务将停止音乐播放。

The is primarily intended for when killing the service would be disruptive to the user, e.g. killing a music player service would stop music playing.

您还需要提供一个通知来这是正在进行的部分显示在通知栏的方法。

You'll need to supply a Notification to the method which is displayed in the Notifications Bar in the Ongoing section.

这篇关于安卓:保持后台服务活(preventing过程中死亡)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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