pub struct WakeLock { /* private fields */ }
Expand description
A wake lock is a mechanism to indicate that your application needs to have the device stay on.
To obtain a wake lock, you can use WakeLock::builder
to configure and
create a wake lock, or you can use partial
to create a partial wake
lock configured with reasonable defaults.
Any application using a WakeLock
must request the
android.permission.WAKE_LOCK
permission in an <uses-permission>
element
of the application’s manifest.
Implementations§
Source§impl WakeLock
impl WakeLock
Sourcepub fn builder<T: Into<String>>(tag: T) -> Builder
pub fn builder<T: Into<String>>(tag: T) -> Builder
Create a new builder with the given tag for configuring and creating a wake lock.
§Tags
Your class name (or other tag) for debugging purposes. Recommended naming conventions for tags to make debugging easier:
- use a unique prefix delimited by a colon for your app/library (e.g.
gmail:mytag
) to make it easier to understand where the wake locks comes from. This namespace will also avoid collision for tags inside your app coming from different libraries which will make debugging easier. - use constants (e.g. do not include timestamps in the tag) to make it easier for tools to aggregate similar wake locks. When collecting debugging data, the platform only monitors a finite number of tags, using constants will help tools to provide better debugging data.
- avoid wrapping the tag or a prefix to avoid collision with wake lock
tags from the platform (e.g.
*alarm*
). - never include personally identifiable information for privacy reasons.
Sourcepub fn is_held(&self) -> Result<bool, Error>
pub fn is_held(&self) -> Result<bool, Error>
Returns true if the wake lock has outstanding references not yet released.
Sourcepub fn acquire(&self) -> Result<Guard<'_>, Error>
pub fn acquire(&self) -> Result<Guard<'_>, Error>
Acquire the wake lock and force the device to stay on at the level that was requested when the wake lock was created.
Returns a Guard
which can be used to release the lock. You should
release wake locks when you are done and don’t need the lock anymore. It
is very important to do this as soon as possible to avoid running down
the device’s battery excessively.
Wake locks are reference counted like a semaphore and may be acquired multiple times by the same or a different thread. The wake lock is not released on the device until all acquired references have been released.
§Examples
// Create the wake lock.
let wake_lock = android_wakelock::partial("myapp:mytag")?;
// Start keeping the device awake.
let guard = wake_lock.acquire()?;
// Do some work while the device is awake...
// Release the wake lock to allow the device to sleep again.
drop(guard);