Android Guides | Samples

Android.OS.PowerManager Class

This class gives you control of the power state of the device.

See Also: PowerManager

Syntax

[Android.Runtime.Register("android/os/PowerManager", DoNotGenerateAcw=true)]
public class PowerManager : Object

Remarks

This class gives you control of the power state of the device.

Flag ValueDescription
PowerManager.ACQUIRE_CAUSES_WAKEUPNormal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
PowerManager.ON_AFTER_RELEASEIf this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.

Device battery life will be significantly affected by the use of this API. Do not acquire NoType:android/os/PowerManager$WakeLock;Href=../../../reference/android/os/PowerManager.WakeLock.htmls unless you really need them, use the minimum levels possible, and be sure to release them as soon as possible.

You can obtain an instance of this class by calling Context.GetSystemService(String).

The primary API you'll use is PowerManager.NewWakeLock(WakeLockFlags,String). This will create a NoType:android/os/PowerManager$WakeLock;Href=../../../reference/android/os/PowerManager.WakeLock.html object. You can then use methods on the wake lock object to control the power state of the device.

In practice it's quite simple:

java Example

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
 wl.acquire();
   ..screen will stay on during this section..
 wl.release();

The following wake lock levels are defined, with varying effects on system power. These levels are mutually exclusive - you may only specify one of them.

Flag ValueCPUScreenKeyboard
PowerManager.PARTIAL_WAKE_LOCKOn*OffOff
PowerManager.SCREEN_DIM_WAKE_LOCKOnDimOff
PowerManager.SCREEN_BRIGHT_WAKE_LOCKOnBrightOff
PowerManager.FULL_WAKE_LOCKOnBrightBright

*If you hold a partial wake lock, the CPU will continue to run, regardless of any display timeouts or the state of the screen and even after the user presses the power button. In all other wake locks, the CPU will run, but the user can still put the device to sleep using the power button.

In addition, you can add two more flags, which affect behavior of the screen only. These flags have no effect when combined with a PowerManager.PARTIAL_WAKE_LOCK.

Any application using a WakeLock must request the android.permission.WAKE_LOCK permission in an <uses-permission> element of the application's manifest.

[Android Documentation]

Requirements

Namespace: Android.OS
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1

The members of Android.OS.PowerManager are listed below.

See Also: Object

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Fields

const
ActionPowerSaveModeChangedString. Intent that is broadcast when the state of PowerManager.IsPowerSaveMode changes.

Public Properties

[read-only]
IsInteractiveBoolean. Returns true if the device is in an interactive state.
[read-only]
IsPowerSaveModeBoolean. Returns true if the device is currently in power save mode.
[read-only]
IsScreenOnBoolean. Returns true if the device is in an interactive state.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

static
FromContext(Context) : PowerManager
GoToSleep(Int64)
Forces the device to go to sleep.
IsWakeLockLevelSupported(Int32) : Boolean
Returns true if the specified wake lock level is supported.
NewWakeLock(WakeLockFlags, String) : PowerManager+WakeLock
Creates a new wake lock with the specified level and flags.
Reboot(String)
Reboot the device.
UserActivity(Int64, Boolean)
Notifies the power manager that user activity happened.
WakeUp(Int64)
Forces the device to wake up from sleep.