android 7.1 上wifi 热点 上的接口变化

在android 7.1 之前 设置wifi ap方法如下:

WifiManager mWifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
mWifiManager.setWifiApEnabled(null, true)

android 7.1后 api 发生了变化 用 setWifiApEnabled打开会导致 连接热点失败,通过在 android 原生setting 查看源码 发现 7.1后 改用

import static android.net.ConnectivityManager.TETHERING_WIFI;private Handler mHandler = new Handler();
private OnStartTetheringCallback mStartTetheringCallback;...................mStartTetheringCallback = new OnStartTetheringCallback(this);
ConnectivityManager mCm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);mCm.startTethering(TETHERING_WIFI, true, mStartTetheringCallback, mHandler);private static final class OnStartTetheringCallback extendsConnectivityManager.OnStartTetheringCallback {final WeakReference<TetherSettings> mTetherSettings;OnStartTetheringCallback(TetherSettings settings) {mTetherSettings = new WeakReference<TetherSettings>(settings);}@Overridepublic void onTetheringStarted() {update();}@Overridepublic void onTetheringFailed() {update();}private void update() {TetherSettings settings = mTetherSettings.get();if (settings != null) {settings.updateState();}}}

这里7.1采用ConnectivityManager类的 startTethering方法 我们看下framework源码里面 startTethering原型

    @SystemApi  public void startTethering(int type, boolean showProvisioningUi,final OnStartTetheringCallback callback) {startTethering(type, showProvisioningUi, callback, null);}@SystemApipublic void startTethering(int type, boolean showProvisioningUi,final OnStartTetheringCallback callback, Handler handler) {ResultReceiver wrappedCallback = new ResultReceiver(handler) {@Overrideprotected void onReceiveResult(int resultCode, Bundle resultData) {if (resultCode == TETHER_ERROR_NO_ERROR) {callback.onTetheringStarted();} else {callback.onTetheringFailed();}}};try {mService.startTethering(type, wrappedCallback, showProvisioningUi);} catch (RemoteException e) {Log.e(TAG, "Exception trying to start tethering.", e);wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);}}@SystemApipublic void stopTethering(int type) {try {mService.stopTethering(type);} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}

startTethering方法参数官方具体解释如下: 
/** 
* Runs tether provisioning for the given type if needed and then starts tethering if 
* the check succeeds. If no carrier provisioning is required for tethering, tethering is 
* enabled immediately. If provisioning fails, tethering will not be enabled. It also 
* schedules tether provisioning re-checks if appropriate. 

* @param type The type of tethering to start. Must be one of 
* {@link ConnectivityManager.TETHERING_WIFI}, 
* {@link ConnectivityManager.TETHERING_USB}, or 
* {@link ConnectivityManager.TETHERING_BLUETOOTH}. 
* @param showProvisioningUi a boolean indicating to show the provisioning app UI if there 
* is one. This should be true the first time this function is called and also any time 
* the user can see this UI. It gives users information from their carrier about the 
* check failing and how they can sign up for tethering if possible. 
* @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller 
* of the result of trying to tether. 
* @param handler {@link Handler} to specify the thread upon which the callback will be invoked. 
* @hide

我们 代开wifi 热点 用 ConnectivityManager.TETHERING_WIFI 
显示UI更新 ,这里用到 ConnectivityManager.OnStartTetheringCallback 
由于这是一个静态的类,我们可以用过弱引用找到父类的 一些方法 和 Context等 
来更新 打开热点的结果

      private static final class OnStartTetheringCallback extendsConnectivityManager.OnStartTetheringCallback {final WeakReference<TetherSettings> mTetherSettings;OnStartTetheringCallback(TetherSettings settings) {mTetherSettings = new WeakReference<TetherSettings>(settings);}@Overridepublic void onTetheringStarted() {update();}@Overridepublic void onTetheringFailed() {update();}private void update() {TetherSettings settings = mTetherSettings.get();//**通过get方法找到父类的引用 并调用方法**if (settings != null) {settings.updateState();}}}

最后在 7.1上面通过修改setWifiApEnabled方法 并成功连接到wifi ap 上网

PS: android7.1 startTethering方法是系统api 只能在系统源码里面编译 自己做了个jar包 以及导入的方法 还有demo apk 方便使用eclipse 的童鞋使用

注意:很多童鞋反应没有作用,请 ps | grep com.example.wifiap7 看下uid是否为 system , 7.1权限更严格.必须是system用户才能控制wifi热点,附件里面提供的platform签名文件为android原生的,其他厂家可能自己有改

android 7.1 上wifi 热点 上的接口变化相关推荐

  1. linux远程win7无法连接wifi,苹果电脑win7系统连不上wifi热点深度纯净

    有时候系统出现某种错误也可能会导致系统无法识别到硬盘,使用系统安装盘对系统进行一下修复或者重新安装新的系统购买电脑的时候,销售人员应该和你说过电脑电源保养方法,可能没有小编讲解的这么专业,他可能说充电 ...

  2. 手机连上无线却手机不显示连接服务器,手机连接上wifi却上不了网该怎么处理...

    为什么手机连接上wifi却上不了网?有不少朋友都遇到过这个的问题,即手机能够搜索到wifi信号,而且也能够连接到wifi信号,但是就是不能够上网.出现这样的情况,多半是无线路由器设置有问题导致的.下面 ...

  3. 笔记本wife DNS服务器未响应,为什么手机已经连接上wifi还是上不了网?

    为什么手机已经连接上wifi还是上不了网?有不少朋友都遇到过这个的问题,即手机能够搜索到wifi信号,而且也能够连接到 为什么手机已经连接上wifi还是上不了网?有不少朋友都遇到过这个的问题,即手机能 ...

  4. android xp wifi连接不上,【能连上wifi但上不了网必看】XP连安卓详细教程

    本帖最后由 octopuslhy 于 2014-9-19 23:27 编辑 本着奉献的精神,本人尝试多款软件都不能在不刷机和不重装系统的条件下用XP连接安卓的设备!在用360后巧妙连接,第二次打开电脑 ...

  5. 手机连上wifi却上不了网的原因

    在目前导致手机连上了wiFi之后却上不了网的原因有多种: 01 比如说 wiFi它本身的问题,因为wiFi它是以一种信号的形式来进行传播的,而这时如果信号在传播的过程中产生一定的此影响的话,那么它的信 ...

  6. android获取ap热点名,android通过反射获取wifi热点ap的ssid和password

    通过查看android5.1的源码,其实wifi热点的ssid和password已经在WifiConfiguration的构造方法里面初始化了,但是是hide的api, 第三方开发者调不到. /** ...

  7. android获取已连上热点设备名称,2、android获取连接到手机热点上的设备的信息

    转自:http://blog.csdn.net/beijingshi1/article/details/9119297 最近开发一个项目,遇到一个问题,在手机开启热点的情况下,想要获取是哪个设备已经连 ...

  8. android 搜索不到wifi热点,新版wifi共享精灵xp安卓手机搜不到热点解决方法介绍

    如果您安装了我们的新版wifi共享精灵软件启动成功,出现手机搜不到热点问题. 这里小编给大家综合一下你们的问题,最后来告诉大家怎么解决这个问题. 首先,我们要按三种常出现的情况讨论: XP系统--安卓 ...

  9. iphone连上wifi却上不了网_必收藏为什么手机信号满格,却上不了网,4招为你解决...

    关键时候掉链子! 明明手机信号满格, 怎么突然间就上不了网, 发不出消息? 第一反应,是网络故障了? 那可不一定哦, Now,是小编闪亮登场的时候了! ↓ ↓ ↓ 01 检查手机网络信号 不要以为信号 ...

最新文章

  1. 如何在sqlite3连接中创建并调用自定义函数
  2. I/O复用函数的使用——epoll
  3. 01_2_数字基带传输及其频谱特性
  4. 读取打印TFRecord格式数据
  5. for循环false 终止 python_python3.5.1给用户3次无效的尝试,然后终止pgm(Simple FOR循环)...
  6. python123判断性别程序_听说胎心能够预测宝宝性别?这是真的吗?
  7. 论文赏析[EMNLP19]用控制任务来设计并解释探测任务
  8. jQuery将json对象转为字符串,将json字符串转为对象
  9. 计算机视觉教程7-3:Openpose配置与实践
  10. Kali Linux上最佳安全测试工具
  11. springboot 整合mybatis,pagehelper。测试类。
  12. 检测数据库连接泄漏的最佳方法
  13. 翟菜花:从美团配送新品牌发布,看即时配送行业奇点何时到来
  14. 【PM】产品需求文档PRD的一般格式
  15. android 模糊线条,android 线条选择器
  16. 前后端交互之——AJAX提交
  17. Tableau中的LOOKUP函数
  18. 225页10万字政务大数据能力平台项目建议书
  19. 【安全知识分享】2021年安全生产月活动方案(附下载)
  20. oracle截取日期函数

热门文章

  1. 如何让程序一直运行_VBA程序正式运行前,要该如何进行调试呢?
  2. Linux C语言连接MySQL 增删改查操作
  3. 处理器不同模式下寄存器
  4. [C++] - private static成员函数
  5. wr703n 4m固件 带打印机服务器_我的WIFI打印机
  6. python爬虫实现方式_python爬虫的实现方法
  7. ORA-39181: Only partial table data may be exported due to fine grain access control on OE.PURCHAS
  8. 解决 IDEA 中src下xml等资源文件无法读取的问题
  9. 『嗨威说』数据结构 - 第七章学习内容小结
  10. 【源码】otter工程结构