@ThreadSafe public final class PartialWakeLock extends Object
WakeLocks.
Users of this class must have the permission
Manifest.permission.WAKE_LOCK.
| Modifier and Type | Method and Description |
|---|---|
void |
acquireLock()
Acquire a partial
WakeLock. |
void |
acquireLockIfNotHeld()
Similar to
acquireLock(), but only acquires a lock if
isHeld() returns false. |
static Map<String,Long> |
dumpWakeLockUsage()
Dumps cumulative WakeLock usage from this class and
PartialWakeLockForService. |
boolean |
isHeld()
Determine whether a
WakeLock is held. |
static PartialWakeLock |
newInstance(Context context,
String lockName,
boolean isReferenceCounted)
Constructs a new
PartialWakeLock. |
void |
releaseLock()
Release a
WakeLock previously acquired. |
void |
releaseLockIfHeld()
Like
releaseLock() but only releases if isHeld()
returns true. |
String |
toString() |
public static Map<String,Long> dumpWakeLockUsage()
PartialWakeLockForService.
This is useful to debug WakeLock usage.public static PartialWakeLock newInstance(Context context, String lockName, boolean isReferenceCounted)
PartialWakeLock.
It is recommended that
applications use a finite number of values for lockName, as an unbounded
number of names would create a memory leak. For example, consider
a background service. Using the name "my_service_lock" every time the service is started
would be better than "my_service_lock_%d" where %d is incremented
every time the service starts. Internally this class maintains a historical count of lock
durations to enable dumpWakeLockUsage(), so creating an unbounded number of tags
would grow linearly in memory usage.
context - Application context.lockName - a tag for identifying the lock.isReferenceCounted - true if the lock is reference counted. False if
the lock is not reference counted.PartialWakeLock.public void acquireLock()
WakeLock.
This method may be called multiple times. If the lock is reference
counted, then each call to this method needs to be balanced by
a call to releaseLock(). Otherwise if the lock is not reference
counted, then multiple calls have no effect.
public void acquireLockIfNotHeld()
acquireLock(), but only acquires a lock if
isHeld() returns false.public void releaseLock()
WakeLock previously acquired. This method should
balance a call to acquireLock().IllegalStateException - if the WakeLockManager is
underlocked.public void releaseLockIfHeld()
releaseLock() but only releases if isHeld()
returns true. This method will not throw exceptions for being
underlocked.public boolean isHeld()
WakeLock is held.true if a lock is held. Otherwise returns false.