Android通过反射获取热点的状态

源码中,热点的几种状态定义:

public static final int WIFI_AP_STATE_DISABLING = 10; 
public static final int WIFI_AP_STATE_DISABLED = 11; 
public static final int WIFI_AP_STATE_ENABLING = 12; 
public static final int WIFI_AP_STATE_ENABLED = 13; 
public static final int WIFI_AP_STATE_FAILED = 14;

public static boolean isWifiApOn(Context context)
{
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    Method method = null;
    int i = 0;

    try
    {
        method = wifiManager.getClass().getMethod("getWifiApState");
    }
    catch (NoSuchMethodException e)
    {
        e.printStackTrace();
    }
    try
    {
        i = (Integer) method.invoke(wifiManager);
    }
    catch (IllegalAccessException e)
    {
        e.printStackTrace();
    }
    catch (InvocationTargetException e)
    {
        e.printStackTrace();
    }
    
    return i == 13; // 13 为已打开
}

猜你喜欢

转载自blog.csdn.net/LikeSidehu/article/details/81483841