Wifi怎么不休眠呢?
当设备熄屏以后,只要打开了wifi,电流一直是20毫安左右,怎么都降不下来。
wifi的休眠策略,可以在设置里配置的。
有那么几个选项:
/**
* Value for {@link #WIFI_SLEEP_POLICY} to use the default Wi-Fi sleep
* policy, which is to sleep shortly after the turning off
* according to the {@link #STAY_ON_WHILE_PLUGGED_IN} setting.
*/
public static final int WIFI_SLEEP_POLICY_DEFAULT = 0;
/**
* Value for {@link #WIFI_SLEEP_POLICY} to use the default policy when
* the device is on battery, and never go to sleep when the device is
* plugged in.
*/
public static final int WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED = 1;
/**
* Value for {@link #WIFI_SLEEP_POLICY} to never go to sleep.
*/
public static final int WIFI_SLEEP_POLICY_NEVER = 2;
看到这里,大家应该知道了,只要修改了数据库里的值,wifi的地方观察到改变了,就会去更新对应休眠策略。
重点是啥呢?
重点是:改了没有作用
那怎么办?
源码就在我手上,那还能怎么样?
就看呗,看它是在哪里使用的。
于是我找呀找,找到了在frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiController.java
里面观察着数据库的变化:
/**
* Observes changes to wifi sleep policy
*/
private void registerForWifiSleepPolicyChange(Handler handler) {
ContentObserver contentObserver = new ContentObserver(handler) {
@Override
public void onChange(boolean selfChange) {
readWifiSleepPolicy();
}
};
mFacade.registerContentObserver(mContext,
Settings.Global.getUriFor(Settings.Global.WIFI_SLEEP_POLICY), false,
contentObserver);
}
也就是设置那边修改了策略,这里就会去readWifiSleepPolicy
,读取最新的值,但是:
private void readWifiSleepPolicy() {
// This should always set to default value because the settings menu to toggle this
// has been removed now.
mSleepPolicy = Settings.Global.WIFI_SLEEP_POLICY_NEVER;
}
问题就在这里了,我这套8.1的祖传代码被以前的人在这里硬编码了,不管你在设置里修改成啥,不管你数据库是怎么变化的,它这里都是Settings.Global.WIFI_SLEEP_POLICY_NEVER
这个值意味着什么呢?
/**
* Determines whether the Wi-Fi chipset should stay awake or be put to
* sleep. Looks at the setting for the sleep policy and the current
* conditions.
*
* @see #shouldDeviceStayAwake(int)
*/
private boolean shouldWifiStayAwake(int pluggedType) {
if (mSleepPolicy == Settings.Global.WIFI_SLEEP_POLICY_NEVER) {
// Never sleep
return true;
} else if ((mSleepPolicy == Settings.Global.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED) &&
(pluggedType != 0)) {
// Never sleep while plugged, and we're plugged
return true;
} else {
// Default
return shouldDeviceStayAwake(pluggedType);
}
}
意味着返回true
从名字上就可以看出,这个方法是用来决定wifi是否长眠还是长活。如果返回true,表示可以一直或者,如果false,表示可以sleep.
这个值在哪里使用呢?
case CMD_SCREEN_OFF:
mScreenOff = true;
/*
* Set a timer to put Wi-Fi to sleep, but only if the screen is off
* AND the "stay on while plugged in" setting doesn't match the
* current power conditions (i.e, not plugged in, plugged in to USB,
* or plugged in to AC).
*/
if (!shouldWifiStayAwake(mPluggedType)) {
//Delayed shutdown if wifi is connected
if (mNetworkInfo.getDetailedState() ==
NetworkInfo.DetailedState.CONNECTED) {
if (DBG) Slog.d(TAG, "set idle timer: " + mIdleMillis + " ms");
mAlarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + mIdleMillis, mIdleIntent);
} else {
sendMessage(CMD_DEVICE_IDLE);
}
}
break;
到这里,基本上就清晰了这个流程了。
--->否--->不作为
收到熄屏的通知---->判断是否要保持活跃
--->是--->判断当前是否有连接--->有则定一个alarm暂时继续使用网络,15分钟后再进入IDLE,如果没有连接直接IDLE
这样子呢,就可以根据客户的要求去改wifi的休眠策略了。
- 是否要保持wifi活跃,即使休眠了
- alarm的时间,也就是上面这个为true,网络连接上的状态下,多次才去让wifi休眠
- 触发wifi保持活跃的条件
mStayAwakeConditions
,可以搜索一下这个源码
这么几个条件控制着wifi是否保活。