If we are running a task and it needs to complete as soon as possible, we can prevent the device's CPU from going to sleep. This allows the task to finish without interruption.
To prevent the CPU from going to sleep, we can use a wake lock:
We will need to ensure that the Xamarin Support Library v4 NuGet or component is installed into the project if we are going to be using the
WakefulBroadcastReceiver
type.Before we can prevent the CPU from going to sleep with a wake lock, we need permission to do so:
[assembly: UsesPermission(Manifest.Permission.WakeLock)]
The recommended way to get hold of a wake lock is to use an instance of
WakefulBroadcastReceiver
and invoke theStartWakefulService()
method:[BroadcastReceiver] public class CriticalReceiver : WakefulBroadcastReceiver { public override void OnReceive( Context context, Intent intent) { var serviceIntent = new Intent( context, typeof(CriticalService)); StartWakefulService(context, serviceIntent...