一切由wifi_sleep_policy提示开始

title: 一切由wifi_sleep_policy提示开始
date: 2016-02-17 12:25:59

tags: Android安全

今天在修改一个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)
    02-17 10:55:49.059 W/System.err:     at xxx.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设置无关,但是这一轮的查找,也让我了解了一些东西 ,今天没有白活 。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值