问题
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 )
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 :
- 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 ?
( OS : Android 5.x )
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 );
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;
}
回答1:
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?
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).
来源:https://stackoverflow.com/questions/37811079/service-is-not-waking-up-device-as-it-should