Wifi网络操作
2019-04-14 本文已影响0人
艾满
相关权限:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
改变WIFI网卡的状态,需要通过WifiManager对象来进行:
WifiManager wifiManager=(WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
WIFI的状态,是定义在WifiManager内部的变量 :
public static final int WIFI_STATE_DISABLED;//关闭1
public static final int WIFI_STATE_DISABLING;//正在关闭0
public static final int WIFI_STATE_ENABLED;//打开3
public static final int WIFI_STATE_ENABLING;//正在打开2
public static final int WIFI_STATE_UNKNOWN;//未知4
获取网卡当前的状态:
wifiManager.getWifiState();
打开WIFI
wifiManager.setWifiEnabled(true);
关闭WIFI
wifiManager.setWifiEnabled(false);