1.什么是WIFI
Wi-Fi 原先是无线保真的缩写,Wi-Fi 的英文全称为wireless fidelity,读音为waifai(拼音读法,均为一声),英文音标/ˈwaɪfaɪ/, wireless [英][ˈwaɪəlɪs] fidelity[英] [fiˈdeliti]。在无线局域网的范畴是指“无线相容性认证”,实质上是一种商业认证,同时也是一种无线联网的技术,以前通过网线连接电脑,而现在则是通过无线电波来连网;常见的就是一个无线路由器,那么在这个无线路由器的电波覆盖的有效范围都可以采用WIFI连接方式进行联网,如果无线路由器连接了一条ADSL线路或者别的上网线路,则又被称为“热点”。
2.获取WIFI网卡的状态
WIFI网卡的状态是由一系列的整形常量来表示的:
1.WIFI_STATE_DISABLED: WIFI网卡不可用操作 1
2.WIFI_STATE_DISABLING: WIFI正在关闭 0
3.WIFI_STATE_ENABLED: WIFI网卡可用 3
4.WIFI_STATE_ENABLING: WIFI网卡正在打开 2
5.WIFI_STATE_UNKNOWN: 未知网卡状态 4
3.操作WIFI所需要的权限
CHANGE_NETWORK_STATE :允许修改网络状态的权限。
CHANGE_WIFI_STATE:允许修改WIFI状态的权限。
ACCESS_NETWORK_STATE:允许访问网络状态的权限。
ACCESS_WIFI_STATE:允许访问WIFI状态的权限。
4.改变WIFI网卡的状态
对WIFI网卡进行操作需要通过WifiManager对象来进行,
获取该对象的方法如下:
WifiManager wifiManager =
(WifiManager)Context.getSystemService(Service.WIFI_SERVICE);
打开WIFI网卡
wifiManager.setWifiEnabled(true);
关闭 WIFI网卡
wifiManager.setWifiEnabled(false);
获取网卡当前的状态
wifiManager.getWifiState();
main.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello" />
11
12 <Button
13 android:id="@+id/startWifi"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:text="@string/startWifi"/>
17
18 <Button
19 android:id="@+id/stopWifi"
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:text="@string/stopWifi"/>
23
24 <Button
25 android:id="@+id/checkWifi"
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content"
28 android:text="@string/checkWifi"/>
29
30 </LinearLayout>
strings.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3
4 <string name="hello">Hello World, WifiActivity!</string>
5 <string name="app_name">Wifi</string>
6 <string name="startWifi">启动Wifi</string>
7 <string name="stopWifi">停止Wifi</string>
8 <string name="checkWifi">检测Wifi状态</string>
9
10 </resources>
AndroidManifest.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="gaolei.wifi"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="8" />
8
9 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
10 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
11 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
12 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
13
14 <application
15 android:icon="@drawable/ic_launcher"
16 android:label="@string/app_name" >
17 <activity
18 android:name=".WifiActivity"
19 android:label="@string/app_name" >
20 <intent-filter>
21 <action android:name="android.intent.action.MAIN" />
22
23 <category android:name="android.intent.category.LAUNCHER" />
24 </intent-filter>
25 </activity>
26 </application>
27
28 </manifest>
WifiActivity:
1 package gaolei.wifi;
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.widget.Button;
9 import android.widget.Toast;
10
11 public class WifiActivity extends Activity {
12 private Button startButton = null;
13 private Button stopButton = null;
14 private Button checkButton = null;
15
16 @Override
17 public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.main);
20 startButton = (Button) findViewById(R.id.startWifi);
21 stopButton = (Button) findViewById(R.id.stopWifi);
22 checkButton = (Button) findViewById(R.id.checkWifi);
23
24 startButton.setOnClickListener(new StartListener());
25 stopButton.setOnClickListener(new StopListener());
26 checkButton.setOnClickListener(new CheckListener());
27 }
28
29 private final class StartListener implements View.OnClickListener {
30
31 @Override
32 public void onClick(View v) {
33 WifiManager wifiManager = (WifiManager) WifiActivity.this
34 .getSystemService(Context.WIFI_SERVICE);
35 wifiManager.setWifiEnabled(true);
36 System.out
37 .println("wifi state ----->" + wifiManager.getWifiState());
38 Toast.makeText(WifiActivity.this,
39 "当前Wifi网卡状态为" + wifiManager.getWifiState(), 1);
40
41 }
42
43 }
44
45 private final class StopListener implements View.OnClickListener {
46
47 @Override
48 public void onClick(View v) {
49 WifiManager wifiManager = (WifiManager) WifiActivity.this
50 .getSystemService(Context.WIFI_SERVICE);
51 wifiManager.setWifiEnabled(false);
52 System.out
53 .println("wifi state ----->" + wifiManager.getWifiState());
54 Toast.makeText(WifiActivity.this,
55 "当前Wifi网卡状态为" + wifiManager.getWifiState(), 1);
56
57 }
58
59 }
60
61 private final class CheckListener implements View.OnClickListener {
62
63 @Override
64 public void onClick(View v) {
65 WifiManager wifiManager = (WifiManager) WifiActivity.this
66 .getSystemService(Context.WIFI_SERVICE);
67 System.out
68 .println("wifi state ----->" + wifiManager.getWifiState());
69 Toast.makeText(WifiActivity.this,
70 "当前Wifi网卡状态为" + wifiManager.getWifiState(), 1);
71 }
72
73 }
74 }