O'Reilly logo

Xamarin Mobile Development for Android Cookbook by Matthew Leibowitz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Critical services

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.

How to do it...

To prevent the CPU from going to sleep, we can use a wake lock:

  1. 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.
  2. 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)]
  3. The recommended way to get hold of a wake lock is to use an instance of WakefulBroadcastReceiver and invoke the StartWakefulService() method:
    [BroadcastReceiver] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required