android 7.1 上wifi 热点 上的接口变化
来源:互联网 发布:linux安装hadoop2.7 编辑:程序博客网 时间:2024/06/09 19:47
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 extends ConnectivityManager.OnStartTetheringCallback { final WeakReference<TetherSettings> mTetherSettings; OnStartTetheringCallback(TetherSettings settings) { mTetherSettings = new WeakReference<TetherSettings>(settings); } @Override public void onTetheringStarted() { update(); } @Override public 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); } @SystemApi public void startTethering(int type, boolean showProvisioningUi, final OnStartTetheringCallback callback, Handler handler) { ResultReceiver wrappedCallback = new ResultReceiver(handler) { @Override protected 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); } } @SystemApi public 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 extends ConnectivityManager.OnStartTetheringCallback { final WeakReference<TetherSettings> mTetherSettings; OnStartTetheringCallback(TetherSettings settings) { mTetherSettings = new WeakReference<TetherSettings>(settings); } @Override public void onTetheringStarted() { update(); } @Override public 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 的童鞋使用
android 7.1 wifi热点Demo
- android 7.1 上wifi 热点 上的接口变化
- 自动连接上wifi的Ap热点
- 在WIN7系统的笔记本上建立WIFI热点
- 在Virtualbox的Ubuntu上创建无线(WiFi)热点
- Wifi连接热点会连接上以保存的网络
- 在windows7上开启虚拟wifi热点
- 在windows7上开启虚拟wifi热点
- 在windows7上开启虚拟wifi热点
- Win7上发布虚拟wifi热点
- android上的WIFI模块
- android:wifi热点的创建
- MT7601U在smart210上AP热点的创建(小度wifi,360随身WIFI 2代,小米wifi)
- android开发步步为营之48:通过WifiManager自动连上某个wifi热点
- 关于自己笔记本上的无线网卡可设置wifi热点的二三事儿!
- WIFI 在android上的应用
- 为了defy能上我的笔记本wifi热点,我累坏了
- 在pcDuino上编译安装自己的内核+RT3070相关+WiFi热点
- Ubuntu系统上使用笔记本架设无线网络(做wifi热点)
- Linux+django 连接SqlServer的问题
- 木马病毒隐身穿墙术解密之花指令和终止进程
- 欢迎使用CSDN-markdown编辑器
- 简单工厂模式
- 软件工程网络课程作业二:命令行菜单小程序
- android 7.1 上wifi 热点 上的接口变化
- 解决Myeclipse破解run.bat无法运行!
- Java运算符_动力节点Java学院整理
- 构造.析构.深拷贝和浅拷贝
- forword与redirect的区别
- java中方法详解
- JavaScript基础之选择器
- Android中apk多渠道打包----友盟打包实现步骤
- JavaScript实现回到顶部/底部的两种方法