本文转自http://blog.csdn.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.access 500(A.java:96)021710:55:49.059W/System.err:atxxx.A.xxx 2.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)

更多相关文章

  1. Android开发EditText属性
  2. ViewPager
  3. android 用代码编写linearlayout布局
  4. android调用邮件应用发送email
  5. android 的popwindow弹窗
  6. Android:ImageView 设置图片
  7. goolge 地图地址位置解析
  8. Android(安卓)studio中关于 No cached version of **** availabl
  9. android listview 三种适配器设置

随机推荐

  1. 结合Spark讲一下Flink的runtime
  2. CaaS在微服务开发运维中的最佳实践
  3. 软件供应链***(依赖关系混淆***)正在破坏你
  4. iOS开发快捷键总览!
  5. 海量数据处理之bitmap
  6. Spark SQL用UDF实现按列特征重分区
  7. libp2p-rs v0.2.1&0.2.2版本介绍
  8. markdown基础语法使用
  9. 第一天作业
  10. 前端插件:form.js和validate.js的简单入门