Skip to content

Most visited

Recently visited

navigation
Added in API level 1

WifiManager.WifiLock

public class WifiManager.WifiLock
extends Object

java.lang.Object
   ↳ android.net.wifi.WifiManager.WifiLock


Allows an application to keep the Wi-Fi radio awake. Normally the Wi-Fi radio may turn off when the user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock is released. Multiple applications may hold WifiLocks, and the radio will only be allowed to turn off when no WifiLocks are held in any application.

Before using a WifiLock, consider carefully if your application requires Wi-Fi access, or could function over a mobile network, if available. A program that needs to download large files should hold a WifiLock to ensure that the download will complete, but a program whose network usage is occasional or low-bandwidth should not hold a WifiLock to avoid adversely affecting battery life.

Note that WifiLocks cannot override the user-level "Wi-Fi Enabled" setting, nor Airplane Mode. They simply keep the radio from turning off when Wi-Fi is already on but the device is idle.

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

Summary

Public methods

void acquire()

Locks the Wi-Fi radio on until release() is called.

boolean isHeld()

Checks whether this WifiLock is currently held.

void release()

Unlocks the Wi-Fi radio, allowing it to turn off when the device is idle.

void setReferenceCounted(boolean refCounted)

Controls whether this is a reference-counted or non-reference-counted WifiLock.

void setWorkSource(WorkSource ws)
String toString()

Returns a string containing a concise, human-readable description of this object.

Protected methods

void finalize()

Invoked when the garbage collector has detected that this instance is no longer reachable.

Inherited methods

From class java.lang.Object

Public methods

acquire

Added in API level 1
void acquire ()

Locks the Wi-Fi radio on until release() is called. If this WifiLock is reference-counted, each call to acquire will increment the reference count, and the radio will remain locked as long as the reference count is above zero. If this WifiLock is not reference-counted, the first call to acquire will lock the radio, but subsequent calls will be ignored. Only one call to release() will be required, regardless of the number of times that acquire is called.

isHeld

Added in API level 1
boolean isHeld ()

Checks whether this WifiLock is currently held.

Returns
boolean true if this WifiLock is held, false otherwise

release

Added in API level 1
void release ()

Unlocks the Wi-Fi radio, allowing it to turn off when the device is idle. If this WifiLock is reference-counted, each call to release will decrement the reference count, and the radio will be unlocked only when the reference count reaches zero. If the reference count goes below zero (that is, if release is called a greater number of times than acquire()), an exception is thrown. If this WifiLock is not reference-counted, the first call to release (after the radio was locked using acquire()) will unlock the radio, and subsequent calls will be ignored.

setReferenceCounted

Added in API level 1
void setReferenceCounted (boolean refCounted)

Controls whether this is a reference-counted or non-reference-counted WifiLock. Reference-counted WifiLocks keep track of the number of calls to acquire() and release(), and only allow the radio to sleep when every call to acquire() has been balanced with a call to release(). Non-reference-counted WifiLocks lock the radio whenever acquire() is called and it is unlocked, and unlock the radio whenever release() is called and it is locked.

Parameters
refCounted boolean: true if this WifiLock should keep a reference count

setWorkSource

Added in API level 9
void setWorkSource (WorkSource ws)

Parameters
ws WorkSource

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
String a printable representation of this object.

Protected methods

finalize

Added in API level 1
void finalize ()

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.