android wifi 休眠策略,Android wifi休眠策略
本文轉自http://blog..net/wwwwap2008/article/details/51783138
最近在項目里發現一段logcat:
W/Settings: Setting wifi_sleep_policy has moved from android.provider.Settings.System to android.provider.Settings.Global, value is unchanged.
后來檢查定位到如下代碼:
/**
* @brief 臨時保存系統休眠時的wifi連接策略
* @param context 上下文
*/
public static void setWifiDormancy()
{
Context context = MyApplication.getInstance();
try {
int value = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
PreferenceMgr.getInstance().setIntConfigValue(Constants.CONFIG_WIFI_SLEEP_POLICY_DEFAULT, value);
if(Settings.System.WIFI_SLEEP_POLICY_NEVER != value)
{
Settings.System.putInt(context.getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
Log.i(TAG, "setWifiDormancy->value = " + Settings.System.WIFI_SLEEP_POLICY_NEVER);
}
} catch (Exception e) {
LogUtil.e(TAG, "setWifiDormancy->exception: %s", e.getMessage());
}
}
由logcat可見,我的項目里面的這段代碼沒有生效,查到如下這篇文章很有幫助,所以轉發到我的博客里:
今天在修改一個bug,在A界面開啟藍牙,并連接設備,開始傳送數據,此時跳轉到頁面B,當關閉頁面B回到A時,藍牙斷開,log日志如下:
02-17 10:55:49.024 W/Settings: Setting wifi_sleep_policy has moved from android.provider.Settings.System to android.provider.Settings.Global, value is unchanged.
02-17 10:55:49.026 W/System.err: java.io.IOException: bt socket closed, read return: -1
02-17 10:55:49.058 W/System.err: at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:508)
02-17 10:55:49.058 W/System.err: at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60)
02-17 10:55:49.058 W/System.err: at xxx.xxx.A.readData(A.java:335)
02-17 10:55:49.058 W/System.err: at xxx.xxx.xxx.access500(A.java:96)02?1710:55:49.059W/System.err:atxxx.A.xxx2.run(A.java:285)
02-17 10:55:49.059 W/System.err: at java.lang.Thread.run(Thread.java:818)
02-17 10:55:49.129 I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@a41b2b9 time:6315482
02-17 10:55:49.912 I/ViewRootImpl: ViewRoot’s Touch Event : ACTION_DOWN
02-17 10:55:50.005 I/ViewRootImpl: ViewRoot’s Touch Event : ACTION_UP
斜體字部分是我感到疑惑的地方,我覺得是導致藍牙斷開的原因。
Setting wifi_sleep_policy has moved from android.provider.Settings.System to android.provider.Settings.Global, value is unchanged.
wifi_sleep_policy 這個設置已經從android.provider.Settings.System 移動到了android.provider.Settings.Global,但是它的值沒有變化。
那就是不同版本的sdk導致的這個提示。于是先查的bing,順便記錄下來,
用到這個系統設置的原因是WIFI在休眠情況下默認是會不連接的,這個時候當我們需要保持連接時,該如何解決,就要把wifi設置為休眠時保持連接。
public void WifiNeverDormancy(Context mContext)
{
ContentResolver resolver = mContext.getContentResolver();
int value = Settings.System.getInt(resolver, Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
final SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(mContext);
Editor editor = prefs.edit();
editor.putInt(mContext.getString(R.string.wifi_sleep_policy_default), value);
editor.commit();
if(Settings.System.WIFI_SLEEP_POLICY_NEVER != value)
{
Settings.System.putInt(resolver, Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
}
System.out.println("wifi value:"+value);
}
上面的代碼是將當前系統的wifi設置保存起來,并把他設置成休眠時不斷開。當我們不需要使用網絡的時候再從sharedpreferences中取出來,還原到修改前的模式。代碼如下
/**
* 還原wifi設置
*
* @param context
*/
@SuppressWarnings("deprecation")
public static void reductionWifiSleepPolicy(Context context) {
SharedPreferences sp = AppContext.getSharedPreferences();
int wifi_sleep_policy = sp.getInt("wifi_sleep_policy", 0);
Settings.System.putInt(context.getContentResolver(),
android.provider.Settings.System.WIFI_SLEEP_POLICY,
wifi_sleep_policy);
}
感覺這個些都沒什么問題。
從哪句提示來看,是不是系統找不到android.provider.Settings.System.WIFI_SLEEP_POLICY這個值,而改為了android.provider.Settings.Global.WIFI_SLEEP_POLICY導致的問題呢?
于是我點入Settings.System.WIFI_SLEEP_POLICY 這個值得源碼:
/** @deprecated */
@Deprecated
public static final String WIFI_SLEEP_POLICY = "wifi_sleep_policy";
已經過期了,那用什么代替了呢? 那應該就是android.provider.Settings.Global.WIFI_SLEEP_POLICY了吧。
去官網看看
都過期了
然后再點擊去看看
原來挪到了Settings.Global下,再看看Settings.Global的簡介
Global system settings, containing preferences that always apply identically to all defined users. Applications can read these but are not allowed to write; like the “Secure” settings, these are for preferences that the user must explicitly modify through the system UI or specialized APIs for those values.
你的應用可以去讀這些設置,但是不能去寫。
也就是說,從sdk17開始,這些就是只讀的了,不能寫了。如果你想在代碼里寫可以,而且是判斷如果如果是sdk<17用Settings.system,是sdk>=17 用Settings.Global,但是需要secure的權限,然后你去mainfest.xml中去寫這個權限,as提示你這個權限只能在系統級別的應用中使用。沒法用了。
也就是說從sdk17開始,就無法去代碼設置wifi在休眠下保持連接了。(大家如果有好的方法,可以推薦給我)
最后這個藍牙斷開的問題,和wifi設置無關,但是這一輪的查找,也讓我了解了一些東西 ,今天沒有白活 。
參考
http://developer.android.com/intl/zh-cn/reference/android/provider/Settings.System.html#putInt(android.content.ContentResolver,%20java.lang.String,%20int)
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的android wifi 休眠策略,Android wifi休眠策略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 代码生成 keyhash
- 下一篇: activity 生命周期_从0系统学A