Android的WIFI网络操作的方法 网络状态 操作权限

什么是WIFI

Wi-Fi是一种可以将个人电脑、手持设备(如PDA、手机)等终端以无线方式互相连接的技术。Wi-Fi是一个无线网路通信技术的品牌,由Wi-Fi联盟(Wi-Fi Alliance)所持有。目的是改善基于IEEE 802.11标准的无线网路产品之间的互通性。现时一般人会把Wi-Fi及IEEE 802.11混为一谈。甚至把Wi-Fi等同于无线网际网路。

获取WIFI网卡的状态

WIFI网卡的状态是由一些列的整型常量来表示的: ① WIFI_STATE_DISABLED:Wi-Fi is disabled. ② WIFI_STATE_DISABLING:Wi-Fi is currently being disabled. ③ WIFI_STATE_ENABLED:Wi-Fi is enabled. ④ WIFI_STATE_ENABLING:Wi-Fi is currently being enabled. ⑤ WIFI_STATE_UNKNOWN:Wi-Fi is in an unknown state.

操作WIFI所需要的权限

在AndroidManifest.xml文件中添加如下用户权限:

改变WIFI网卡的状态

//对WIFI网卡进行操作需要通过WifiManager对象来进行,获取该对象的方法如下:
WifiManager wifiManager = (WifiManager)WifiTestActivity.this.getSystemService(Context.WIFI_SERVICE);
//打开WIFI网卡:
wifiManager.setWifiEnabled(true);
//关闭WIFI网卡:
wifiManager.setWifiEnabled(false);
//获取网卡当前的状态:
wifiManager.getWifiState();

public abstract class Context extends Object

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

WIFI_SERVICE:Use with getSystemService(String) to retrieve a WifiManager for handling management of Wi-Fi access.

(特别说明:本文部分内容是在观看marschen的Android视频教程时做的笔记,感谢marschen推出的视频教程,这里也推荐给大家:http://www.marschen.com/portal.php)

arthinking wechat
欢迎关注itzhai公众号