1、 WIFI网卡的状态
WIFI网卡的状态信息都以整型变量的形式存放在 android.net.wifi.WifiManager 类中,有以下状态:
WIFI_STATE_DISABLED
WIFI网卡不可用
WIFI_STATE_DISABLING
WIFI网卡正在关闭
WIFI_STATE_ENABLED
WIFI网卡可用
WIFI_STATE_ENABLING
WIFI网卡正在打开
WIFI_STATE_UNKNOWN
WIFI网卡状态未知
2、 操作WIFI 网卡所需的权限
CHANGE_NETWORK_STATE
允许应用程序改变网络连接状态
CHANGE_WIFI_STATE
允许应用程序改变WIFI 连接状态
ACCESS_NETWORK_STATE
允许应用程序获取网络的状态信息
ACCESS_WIFI_STATE
允许应用程序获得WIFI 的状态信息
3、 改变WIFI 网卡的状态
对WIFI 网卡进行操作需要通过对 WifiManager 对象来进行,获取该对象的方法如下:
复制代码 代码如下:
WifiManager wifiManager=(WifiManager) Context.getSystemService(Service.WIFI_SERVICE);
打开WIFI 网卡:
复制代码 代码如下:
wifiManager.setWifiEnabled(true);
关闭WIFI 网卡
复制代码 代码如下:
wifiManager.setWifiEnabled(false);
获取网卡的当前状态:
复制代码 代码如下:
wifiManager.getWifiState();
示例代码:由于Android模拟器不支持wifi操作,所以例子需要在实体手机上运行
AndroidManifest.xml
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.bym" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WifiActivity" 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-sdk android:minSdkVersion="4" />
<!-- 以下是使用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>
</manifest>
Activity
复制代码 代码如下:
package me.bym;
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 WifiActivity extends Activity {
private Button startWifiButton = null;
private Button shutdownWifiButton = null;
private Button checkStateButton = null;
private WifiManager wifiManager = null;
private String res = ""; // 用来存放操作结果信息的字符串
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startWifiButton = (Button) findViewById(R.id.StartWifiButton);
shutdownWifiButton = (Button) findViewById(R.id.ShutdownWifiButton);
checkStateButton = (Button) findViewById(R.id.CheckStateButton);
startWifiButton.setOnClickListener(new StartWifiListener());
shutdownWifiButton.setOnClickListener(new ShutdownWifiListener());
checkStateButton.setOnClickListener(new CheckStateListener());
}
class StartWifiListener implements OnClickListener {
@Override
public void onClick(View v) {
wifiManager = (WifiManager) WifiActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.setWifiEnabled(true)) {
res = "启动wifi服务成功";
} else {
res = "启动wifi服务失败";
}
Toast.makeText(WifiActivity.this, res, Toast.LENGTH_SHORT).show();
}
}
class ShutdownWifiListener implements OnClickListener {
@Override
public void onClick(View v) {
wifiManager = (WifiManager) WifiActivity.this
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.setWifiEnabled(false)) {
res = "关闭wifi服务成功";
} else {
res = "关闭wifi服务失败";
}
Toast.makeText(WifiActivity.this, res, Toast.LENGTH_SHORT).show();
}
}
class CheckStateListener implements OnClickListener {
@Override
public void onClick(View v) {
wifiManager = (WifiManager) WifiActivity.this
.getSystemService(Context.WIFI_SERVICE);
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_DISABLED:
res = "WIFI已关闭";
break;
case WifiManager.WIFI_STATE_DISABLING:
res = "WIFI正在关闭中";
break;
case WifiManager.WIFI_STATE_ENABLED:
res = "WIFI已启用";
break;
case WifiManager.WIFI_STATE_ENABLING:
res = "WIFI正在启动中";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
res = "未知WIFI状态";
break;
}
Toast.makeText(WifiActivity.this, res, Toast.LENGTH_SHORT).show();
}
}
}