android开发教程之wifi开发示例
本文导语: 1、 WIFI网卡的状态WIFI网卡的状态信息都以整型变量的形式存放在 android.net.wifi.WifiManager 类中,有以下状态:WIFI_STATE_DISABLEDWIFI网卡不可用WIFI_STATE_DISABLINGWIFI网卡正在关闭WIFI_STATE_ENABLEDWIFI网卡可用WIFI_STATE_ENABLINGWIFI网卡正在打开W...
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
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();
}
}
}