1. 什么是wifi?
Wifi是一种无线联网的技术,以前通过网线连接电脑,现在通过无线电波来连网,常见的就是一个无限路由器,那么这个无限路由器的电波覆盖的有效范围都可以采用wifi连接方式进行连网,如果无线路由器连接了一条ADSL线路或者别的上网线路,则又被称为热点。
2. 获取wifi网卡的状态
是由一系列的整形常量来表示的,
WIFI_STATE_DISABLED: 网卡不可用 constant value 1
WIFI_STATE_DISABLING:WIFI正在关闭 constant value 0
WIFI_STATE_ENABLED: WIFI网卡可用 constant value 3
WIFI_STATE_ENABLING : WIFI网卡正在打开 constant value 2
WIFI_STATE_UNKNOWN:未知网卡状态
3. 操作wifi所需要的权限
<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>
4. 改变wifi网卡的状态
改变wifi网卡的状态,
对wifi网卡进行操作,需要通过WifiManager对象来进行,获取该对象的方法如下:
WifiManger
WifiManger =( WifiManger)Context.getSystemService(Service.WIFI_SERVICE);
打开wifi网卡
WifiManger.setWifiEnabled(ture);
关闭wifi网卡
WifiManger.setEnable(false);
获取网卡当前的状态:
WifiManger.getWifiState();
代码如下:
1 package com.wbk.wifi.activity; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.net.wifi.WifiManager; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 import android.widget.Toast;11 12 public class Wifi_wbkActivity extends Activity13 {14 private Button startButton = null;15 private Button stopButton = null;16 private Button checkButton = null;17 18 private WifiManager wifiManager = null;19 20 @Override21 public void onCreate(Bundle savedInstanceState)22 {23 super.onCreate(savedInstanceState);24 setContentView(R.layout.main);25 startButton = (Button) findViewById(R.id.startButton);26 stopButton = (Button) findViewById(R.id.stopButton);27 checkButton = (Button) findViewById(R.id.checkButton);28 startButton.setOnClickListener(new startWifiListener());29 stopButton.setOnClickListener(new stopWifiListener());30 checkButton.setOnClickListener(new checkWifiListener());31 }32 33 // 内部类,需要的是Wifi_wbkActivity的对象,所以Wifi_wbkActivity.this34 class startWifiListener implements OnClickListener35 {36 37 @Override38 public void onClick(View v)39 {40 wifiManager = (WifiManager) Wifi_wbkActivity.this.getSystemService(Context.WIFI_SERVICE);41 wifiManager.setWifiEnabled(true);42 System.out.println("wifi start ----->" + wifiManager.getWifiState());43 Toast.makeText(Wifi_wbkActivity.this, "当前wifi网卡状态为", wifiManager.getWifiState());44 }45 }46 47 class stopWifiListener implements OnClickListener48 {49 50 @Override51 public void onClick(View v)52 {53 wifiManager=(WifiManager)Wifi_wbkActivity.this.getSystemService(Context.WIFI_SERVICE);54 wifiManager.setWifiEnabled(false);55 System.out.println("wifi stop --->"+wifiManager.getWifiState());56 Toast.makeText(Wifi_wbkActivity.this, "当前的wifi状态为", wifiManager.getWifiState());57 58 }59 60 }61 class checkWifiListener implements OnClickListener{62 63 @Override64 public void onClick(View v)65 {66 wifiManager=(WifiManager)Wifi_wbkActivity.this.getSystemService(Context.WIFI_SERVICE);67 System.out.println("wifi state ----->"+wifiManager.getWifiState());68 Toast.makeText(Wifi_wbkActivity.this, "当前的wifi状态为", wifiManager.getWifiState());69 }70 71 }72 73 }
来源:https://www.cnblogs.com/bokun-wang/archive/2011/10/06/2200056.html