服务未按预期方式唤醒设备 [英] Service is not waking up device as it should
问题描述
我的应用程序每秒执行一次重复请求-向服务器询问新数据,如果有,则唤醒设备并运行主应用程序. (这是根据用户的请求,他们不关心电池使用情况,必须每秒运行一次.这是非常关键的操作) (用户允许对设置进行的任何更改都可以帮助您)
My application do a repeating request each second - asking the server for a new data, and if there is such - it wakes up the device and run the main application. ( this is by the request from the users, and they does not care about the battery usage, it is mandatory to run each second. It is Highly Critical Operation ) ( any changes to the settings which could help is allowed by the user )
我正在尝试通过使用AlarmManager
或System.Threading.Timer
(两者都尝试过)来实现这一目标,但是每次我最终遇到以下问题时:
I am curently trying to achieve this by using either AlarmManager
or System.Threading.Timer
( tried with both ), but each time I am ending up with the follow issue :
- 设备在休眠状态下的某个时间点停止请求服务器,并且在某个看起来随机的时间恢复了它的短暂工作.为什么会发生这种情况,以及如何解决此问题?
(操作系统:Android 5.x)
( OS : Android 5.x )
来自Java服务变体的代码(使用AlarmManager)
The code from the variation of the service in Java ( using AlarmManager )
Calendar cal = Calendar.getInstance();
cal.add( Calendar.SECOND, ConfigReader.serviceRepeatInterval );
Intent intent = new Intent( this, SaleService.class );
PendingIntent pintent = PendingIntent.getService( this, 0, intent, 0 );
AlarmManager alarm = ( AlarmManager )getSystemService( Context.ALARM_SERVICE );
alarm.setRepeating( AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), ConfigReader.serviceRepeatInterval, pintent );
该服务的变种形式的代码是使用Timer的C#(Xamarin).
The code from the variation of the service is C# ( Xamarin ) using Timer.
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
if( _powerManager == null )
{
_powerManager = (PowerManager)this.GetSystemService(PowerService);
_wakeLock = _powerManager.NewWakeLock( WakeLockFlags.Full | WakeLockFlags.AcquireCausesWakeup | WakeLockFlags.OnAfterRelease, mySaleServiceWakeLock );
vibrator = (Vibrator)this.GetSystemService( Context.VibratorService );
}
timer = new System.Threading.Timer( getCurrentPendingObjects, null, timerRepeatInterval, Timeout.Infinite );
return StartCommandResult.Sticky;
}
推荐答案
警报触发时,使用AlarmManager.ELAPSED_REALTIME_WAKEUP
或AlarmManager.RTC_WAKEUP
可以唤醒设备.
Using AlarmManager.ELAPSED_REALTIME_WAKEUP
or AlarmManager.RTC_WAKEUP
will wake the device up when the alarm fires.
设备在休眠状态下的某个时候停止请求服务器 并在一些看起来像随机的时间恢复了它的短暂工作. 为什么会发生这种情况,以及如何解决此问题?
The device stop requesting the server at some point while it is slept and at some look-like random time it restore it's work for a short. Why this happens, and how to fix this issue?
这可能是因为您的PendingIntent
正在呼叫Service
.这样,设备可以在执行onStartCommand()
之前返回睡眠状态.
您应该改用BroadcastReceiver
(因为在onReceive()
中保证"了WakeLock
),在onReceive()
中获取了WakeLock
,从此处启动Service
,并从<释放了WakeLock
c5>(在适当的时候).
This might happen because your PendingIntent
is calling a Service
. This way the device can go back to sleep before onStartCommand()
gets executed.
You should use a BroadcastReceiver
instead (since a WakeLock
is "guaranteed" during onReceive()
), aquire a WakeLock
in onReceive()
, start your Service
from there and release your WakeLock
from the Service
, when appropriate.
尽管如此,每隔一秒钟重复一次请求(甚至在设备处于睡眠状态时)似乎是一个糟糕的设计.它将很快耗尽电池电量.
Although, repeating a request every second for an extended period of time (and even when the device is in sleep) seems like a terrible design. It will drain the battery pretty fast.
您应该重新考虑实现,可以改用某种回调机制,或者至少(显着)增加请求之间的间隔.
You should rethink your implementation, maybe use some sort of a callback mechanism instead, or at least increase the intervals between the requests (significantly).
这篇关于服务未按预期方式唤醒设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!