Android Wifi 信息保存与忘记
来源:互联网 发布:一知f君2月13号 编辑:程序博客网 时间:2024/06/06 12:41
最近在做项目的时候,遇到一个需求,在某一个页面内需要连接指定Wifi,当退出这个页面时需要恢复原来的网络状态。于是就想到了退出页面时忘记此时连接的指定wifi。于是,代码开始了
WifiManager mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); mWifiManager.forget()...
等等,为什么forget是红色的,没提示呢???惊叹三秒钟只有赶紧回源码查看,原来是这样的
/** * Delete the network in the supplicant config. * * This function is used instead of a sequence of removeNetwork() * and saveConfiguration(). * * @param config the set of variables that describe the configuration, * contained in a {@link WifiConfiguration} object. * @param listener for callbacks on success or failure. Can be null. * @throws IllegalStateException if the WifiManager instance needs to be * initialized again * @hide */ public void forget(int netId, ActionListener listener) { if (netId < 0) throw new IllegalArgumentException("Network id cannot be negative"); getChannel().sendMessage(FORGET_NETWORK, netId, putListener(listener)); }
看到这个@hide了没,原来这方法被隐藏掉了,看来要用只能用反射了啊,可是还是不甘心,不想用反射,于是就继续找啊找啊,于是在这个方法的注释里找到了这个:
* This function is used instead of a sequence of removeNetwork() * and saveConfiguration().
好吧,我们就看看removeNetwork(),
/** * Remove the specified network from the list of configured networks. * This may result in the asynchronous delivery of state change * events. * @param netId the integer that identifies the network configuration * to the supplicant * @return {@code true} if the operation succeeded */ public boolean removeNetwork(int netId) { try { return mService.removeNetwork(netId); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }
看了上面的注释,大伙都应该明白了。于是问题完美解决,同样的save也用saveConfiguration()替代
/** * Tell the supplicant to persist the current list of configured networks. * <p> * Note: It is possible for this method to change the network IDs of * existing networks. You should assume the network IDs can be different * after calling this method. * * @return {@code true} if the operation succeeded */ public boolean saveConfiguration() { try { return mService.saveConfiguration(); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }
好了,就这样,再查资料的时候找到了有些博客,命名上面方法写的@hide,下面代码还是用forget,不知道最后成功了没。。。
欢迎不同意见的朋友一起留言讨论,共同学习进步。
阅读全文
1 0
- Android Wifi 信息保存与忘记
- Android手机平台wifi信息保存位置以及联网API
- Android Wifi State保存
- android 获取wifi信息
- Android获取WiFi 信息
- android WIFI信息获取
- android WIFI信息获取
- Android--部分wifi信息
- 安卓移除/忘记已保存的WiFi密码
- android 3种方式忘记wifi密码
- android查看已经保存的WIFI密码
- Android查看已保存的WIFI密码
- Android wifi属性简介 及 wifi信息获取(wifi列表、配置信息、热点信息)
- Android与Wifi
- Android -- wifi与socket
- Android获取3G或WiFi流量信息
- android获取wifi信息sample code
- android获取3G或wifi流量信息
- 运用Python实现多种排序的方法
- Firefox下载文件时中文名乱码问题
- MySQL基本练习
- SDK相关问题
- 研究生入门
- Android Wifi 信息保存与忘记
- ccf 交通规划
- Centos 6.9安装docker
- PCA原理及特征脸
- shiro session 监听
- libzlib LNK2019错误之解决方法
- redis | set命令
- P1040 加分二叉树(树形dp)
- I.MX6Q-ipu视频流格式转换