1.概述
最近在做Android盒子开发的时候,在对wifi和热点的开发需求也是比较多的,在项目开发中需要对热点进行开关和对状态的判断,经过查询相关资料找到解决方案WifiManager.java源码,通过WifiManager中的api来获取相关状态,而在其中的getWifiApState() 就是获取热点开关的状态
2.1获取热点状态的两种方法
@SystemService(Context.WIFI_SERVICE)
public class WifiManager {
IWifiManager mService;
/**
* Gets the Wi-Fi enabled state.
* @return One of {
@link #WIFI_AP_STATE_DISABLED},
* {
@link #WIFI_AP_STATE_DISABLING}, {@link #WIFI_AP_STATE_ENABLED},
* {
@link #WIFI_AP_STATE_ENABLING}, {@link #WIFI_AP_STATE_FAILED}
* @see #isWifiApEnabled()
*
* @hide Dont open yet
*/
public int getWifiApState() {
try {
return mService.getWifiApEnabledState();
} catch (RemoteException e) {
return WIFI_AP_STATE_FAILED;
}
}
从getWifiApState()的相关方法可以看出,最终是调用WifiManagerService.java中的getWifiApEnabledState()来获取热点的状态
如果getWifiApState()被隐藏,就得调用反射来获取
public static boolean isWifiApOpen(Context context) {
try {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//通过放射获取 getWifiApState()方法
Method method = manager.getClass().getDeclaredMethod("getWifiApState");
//调用getWifiApState() ,获取返回值
int state = (int) method.invoke(manager);
//通过放射获取 WIFI_AP的开启状态属性
Field field = manager.getClass().getDeclaredField("WIFI_AP_STATE_ENABLED");
//获取属性值
int value =