一,先了解wifi是什么
Wi-Fi是一种可以将个人电脑、手持设备(如PDA、手机)等终端以无线方式互相连接的技术。比较常见的就是一个无线路由器,在它发出的无线电波有效范围内都可以通过wifi方式连接到网络,如果无线路由器连接了一条ADSL线路或者其他的上网线路,则又被称为“热点”。
二,获取wifi网卡状态
wifi的状态就是定义的一系列整型常量,如下:
- wifimanager?=?(WifiManager)TextWifi.this.getSystemService(Context.WIFI_SERVICE);??
- ????????????????wifimanager.getWifiState();??
三,操作wifi的权限
四,更改wifi状态
对wifi网卡进行操作时要用到WifiManager来进行,怎么去获取这个对象呢?
- wifimanager?=?(WifiManager)TextWifi.this.getSystemService(Context.WIFI_SERVICE);??
如何打开wifi网卡呢?
- wifimanager.setWifiEnabled(true);??
如何获取网卡当前状态?
- wifimanager.getWifiState()??
如何关闭wifi网卡呢?
- wifimanager.setWifiEnabled(false);??
五,实例演示,必须在真机上测试才行,模拟器不行。
- import?android.app.Activity;??
- import?android.content.Context;??
- import?android.net.wifi.WifiManager;??
- import?android.os.Bundle;??
- import?android.view.View;??
- import?android.view.View.OnClickListener;??
- import?android.widget.Button;??
- import?android.widget.Toast;??
- ??
- public?class?TextWifi?extends?Activity?{??
- ????private?Button?openWifi?=?null;??
- ????private?Button?getWifiState?=?null;??
- ????private?Button?closeWifi?=?null;??
- ????private?WifiManager?wifimanager?=?null;??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????openWifi?=?(Button)findViewById(R.id.openWifi);??
- ????????openWifi.setOnClickListener(new?OnClickListener()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?onClick(View?v)?{??
- ????????????????wifimanager?=?(WifiManager)TextWifi.this.getSystemService(Context.WIFI_SERVICE);??
- ????????????????wifimanager.setWifiEnabled(true);??
- ????????????????Toast.makeText(TextWifi.this,?"Wifi已开启",?Toast.LENGTH_SHORT).show();??
- ????????????}??
- ????????});??
- ????????getWifiState?=?(Button)findViewById(R.id.getWifiState);??
- ????????getWifiState.setOnClickListener(new?OnClickListener()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?onClick(View?v)?{??
- ????????????????wifimanager?=?(WifiManager)TextWifi.this.getSystemService(Context.WIFI_SERVICE);??
- ????????????????Toast.makeText(TextWifi.this,?"Wifi状态"+wifimanager.getWifiState(),?Toast.LENGTH_SHORT).show();??
- ????????????}??
- ????????});??
- ????????closeWifi?=?(Button)findViewById(R.id.closeWifi);??
- ????????closeWifi.setOnClickListener(new?OnClickListener()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?onClick(View?v)?{??
- ????????????????wifimanager?=?(WifiManager)TextWifi.this.getSystemService(Context.WIFI_SERVICE);??
- ????????????????wifimanager.setWifiEnabled(false);??
- ????????????????Toast.makeText(TextWifi.this,?"Wifi已关闭",?Toast.LENGTH_SHORT).show();??
- ????????????}??
- ????????});??
- ????}??
- }??
- <?xml?version="1.0"?encoding="utf-8"?>??
- <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
- ??????package="android.apps"??
- ??????android:versionCode="1"??
- ??????android:versionName="1.0">??
- ??
- ??
- ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
- ????????<activity?android:name=".TextWifi"??
- ??????????????????android:label="@string/app_name">??
- ????????????<intent-filter>??
- ????????????????<action?android:name="android.intent.action.MAIN"?/>??
- ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
- ????????????</intent-filter>??
- ????????</activity>??
- ??
- ????</application>??
- ????<uses-permission?android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>??
- ????<uses-permission?android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>??
- ????<uses-permission?android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>??
- ????<uses-permission?android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>??
- </manifest>??