Android
对WiFi电源管理
的代码主要在WifiService.java中。如果应用
程序想在屏幕被关掉后继续使用WiFi则可以调用acquireWifiLock来锁住WiFi,该操作会阻止WiFi进入睡眠状态。当应用程序不再使用WiFi时需要调用releaseWifiLock来释放WiFi。之后WiFi可以进入睡眠状态以节省电源。
默认情况下当屏幕被关掉以后,如果没有应用程序在使用WiFi,WiFi会在2分钟后进入睡眠状态。这主要是为防止频繁地改变WiFi的电源模式。
1.
INTRODUCTION
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.
2. API
WifiLock
APIs are implemented in android.net.wifi.WifiManager.
2.1
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.
2.2
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.
2.3
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.
Param: refCounted true if this WifiLock should keep a reference
count
2.4
isHeld()
Checks whether this WifiLock is currently held.
return true if this WifiLock is held, false otherwise
3. EXAMPLE
frameworks/base/services/java/com/android/server/LocationManagerService.java
is a good example for your reference.
Our simple
example is as blew.
import android.os.PowerManager;
import android.net.wifi.WifiManager;
public class WifiLockExample{
private
final Context mContext;
private
PowerManager.WakeLock mWakeLock = null;
private
WifiManager.WifiLock mWifiLock = null;
public
WifiLockExample(Context context){
mContext = context;
// Create a wake lock
PowerManager powerManager = (PowerManager)
mContext.getSystemService(Context.POWER_SERVICE);
mWakeLock =
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
WAKELOCK_KEY);
mWakeLock.setReferenceCounted(true);
// Create a wifi lock
WifiManager wifiManager = (WifiManager)
context.getSystemService(Context.WIFI_SERVICE);
mWifiLock = wifiManager.createWifiLock(WIFILOCK_KEY);
mWifiLock.setReferenceCounted(true);
}
private void
acquire(){
// Acquire wake lock
mWakeLock.acquire();
// Acquire wifi lock
mWifiLock.acquire();
}
private void
release(){
// Release wifi lock
mWifiLock.release();
// Release wake lock
mWakeLock.release();
}
}
分享到:
相关推荐
Android_WiFi电源管理
Android 上wifi和蓝牙模块的初始化过程,wifi bt framwork层和hal层的调用,电源管理介绍
4.4 android电源管理机制的实现 /154 4.5 小结 /187 第5章 驱动的工作原理及实现机制 /188 5.1 显示驱动(framebuffer)/189 5.1.1 framebuffer的工作原理 /189 5.1.2 framebuffer的构架 /190 5.1.3 framebuffer驱动...
4.4 android电源管理机制的实现 /154 4.5 小结 /187 第5章 驱动的工作原理及实现机制 /188 5.1 显示驱动(framebuffer)/189 5.1.1 framebuffer的工作原理 /189 5.1.2 framebuffer的构架 /190 5.1.3 ...
除了标准的Linux内核外,Android还增加了内核的驱动程序:Binder(IPC)驱动、显⽰驱动、输⼊设备驱 动、⾳频系统驱动、摄像头驱动、WiFi驱动、蓝⽛驱动、电源管理。 b) 程序库 (LIBRARIES) 程序库是指可供使⽤的各种...
A13-SOM-WIFI是片上系统模块屏蔽,可为A13-SOM-XXX增加WIFI连接。所有A13信号均可在0.1“孔上使用。...电源管理DCDC GPIO采用0.1“阶梯孔 内置PCB天线 尺寸:(2.4 x 1.3)“〜(6.1 x 4.8)cm 附件资料截图:
轻松实现功能最多的Android-PC遥控器。将您的Android设备 变成Windows PC的WiFi或蓝牙遥控器。...预加载了对90多个程序的支持,其中包括Moue和键盘,媒体播放器,演示文稿,屏幕镜像,电源控制,文件管理器,终端等。
本篇文章适合初学MTK智能机部分的底层驱动学习,虽然是一些简单的介绍,但是对初学者非常好。我就是从这个开始学习的,欢迎一同交流 ...13、电源管理 14、音频 15、I2C 16、耳机 17、USB 18、附加模块
Wifi Analyzer,把你的android手机变成一个 Wi-Fi分析仪。 它可以显示你周围的Wi-Fi信道使用情况,帮助你为你的无线路由选择一个相对空闲的信道以提高连接质量。 本软件需要的权限包括:打开网络接口、访问网络上的...
wifi参数调整,有效提升wifi信号强度 支持google账户同步 增加恢复模式、引导模式、屏幕截图、飞行模式振动模式到关机高级菜单 增加kinguser权限管理,状态栏增加农历显示 增加国内天气源,音量键唤醒屏幕 1%步...
SL8541E是一款高度集成的应用处理器,具有... 专为低功耗ASIC设计和电源管理而开发的专有架构和算法。 独特的技术用于噪声/偏移校准和消除。 总体而言,SL8541E芯片组为Android移动设备提供了一个高性价比的平台。
如果连接不了,请确认 Android 设备与电脑是连接到了同一个 WiFi,然后再次执行 adb connect <device-ip-address> 那一步; 如果还是不行的话,通过 adb kill-server 重新启动 adb 然后从头再来一次试试。 断开...
内置PLL、稳压器和电源管理组件,802.11b 模式下 +20dBm 的输出功率 A-MPDU 、A-MSDU 的聚合和 0.4s 的保护间隔 WiFi @ 2.4GHz,支持 WPA/WPA2 安全模式 支持 AT远程升级 及云端 OTA升级 支持 STA/AP/STA+AP ...
内置PLL、稳压器和电源管理组件,802.11b 模式下+18 dBm的输出功率 A-MPDU 、 A-MSDU 的聚合和 0.4 s的保护间隔 Wi-Fi @ 2.4 GHz,支持 WPA/WPA2 安全模式 支持AT本地升级及云端OTA升级 支持 STA/AP/STA+AP 工作模式...
PMU电源管理单元:DS_ACT8846 DC/DC:AN_SYR827 以太网口变压器:SG24301 通道切换芯片:CBT3257 网卡驱动芯片:RTL8211E(G) 视频数模转换器:ADV7123 HDMI转MIPI :(U18)TC358749XBG 附件资料截图: 可能感兴趣的项目设计: ...
A13-SOM-512是片上系统模块,带有A13 Cortex-A8 ARM处理器,电源管理单元,512MB DDR3内存。所有免费A13信号均可在4个连接器上使用,步长为0.05“.A13-SOM-512具有UART控制台和microSD卡连接器。 A13-SOM-512可以...
ESP8266 / WS2812灯光动画 如何开始 如下图所示连接您的ESP,LED... 这不适用于Android,但是,您应该找到Liana的IP地址(例如,在路由器管理控制台中,或使用 ) 。 然后在Android浏览器中键入“ http:// <IP>”。
x86_ramos_i9_kernel 用于 Ramos i9 平板电脑的非官方 3.10.... USB、触摸屏、视频、声音、MMC、SD 卡、电源管理、wifi、蓝牙、两个摄像头、加速度传感器、充电工作正常。 注意:这里的lcd面板、摄像头、ft5x0x_ts和sm
Android本来就支持WiFi热点,可能有点画蛇添足吧,呵呵。我是偶然发现openwrt还支持这个功能, 所以就研究了下,也许在某些情况下这个功能还是有用的吧。 使用方法: a. 路由器模式开关切换至AP位置。 b. 用USB...