服务不应该当手机处于休眠状态,或活动(用于启动服务)停止不运行 [英] Service shouldn't stop when the phone is sleeping, or the activity (for starting the service) is not running
问题描述
我想运行一个服务来收集加速度传感器信息和当手机睡眠或活动(用于启动服务)未运行,应该不会停止。
我要送启动和停止命令,从菜单中活动的服务。结果
目前我使用的是捆绑服务在活动的过程一样,但问题是,它得到尽快的活动被关闭关闭(返回键pressed)。
I want to run a service to collect the accelerometer sensor information and it shouldn't stop when the phone is sleep or the activity (for starting the service) is not running.
I have to send start and stop commands to the service from the menu activity.
currently I am using a bundled service in the same process of the activity but the problem is that it gets closed as soon as activity is closed (return key pressed).
我想知道如果我使用一个单独的进程,如果没有绑定的活动它甚至会恢复(当活动被关闭)。
I am wondering if I use a separate process it will resume even if there is no bundled activity (when activity is closed).
如果不是,它的服务模式,我应该选择?
If not, which service model should I choose?
推荐答案
您可能正在寻找startService,而不是bindService。
You are probably looking for startService instead of bindService.
<一个href=\"http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29\" rel=\"nofollow\">http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29
然而,即使有startService,没有保证服务将保持运行永远和总是。
However, even with startService, there are no guarantees the service will remain running "forever" and "always".
警告,下面的选项会大量消耗电池。
WARNING, the options below will consume a lot of battery.
您可以增加服务将不会改变优先startforeground停止的机会(需要通知)。
You can increase the chances the service will not be stopped by changing the priority to startforeground (requires a notification).
当屏幕处于关闭状态,以保持服务一直活着,唯一的办法就是使用报警管理与RTC_WAKEUP或ELAPSED_REALTIME_WAKEUP时间表。
While the screen is off, the only way to keep the service "alive all the time" is to use Alarm Manager with an RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP schedules.
较少的电池...
实事求是地讲,没有startForeground,只是使用普通的RTC或ELAPSED_REALTIME报警时间表,你的服务将运行的大部分时间。
Practically speaking, however, without startForeground and just using normal RTC or ELAPSED_REALTIME alarm schedules, your service will run most of the time.
这篇关于服务不应该当手机处于休眠状态,或活动(用于启动服务)停止不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!