Struct WakeLock

Source
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

Source

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.
Source

pub fn is_held(&self) -> Result<bool, Error>

Returns true if the wake lock has outstanding references not yet released.

Source

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);

Trait Implementations§

Source§

impl Debug for WakeLock

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.