一.什么是WIFI
WIFI 就是一种无线联网技术,常见的一个就是无线路由器。那么在这个无线路由器的电波覆盖的有效范围内都可以采用 WIFI 连接的方式进行联网。如果无线路由器连接了一个ADLS线路或者别的联网线路,则又被称为“热点”。
二.获取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) 《WIFI启动需要一段时间》
5. WIFI_STATE_UNKNOWN : 未知网卡状态
三.操作WIFI所需要的权限
CHANG.NETWORK.STATE
CHANG.NETWORK.WIFISTATE
ACCESS.NETWORK.STATE
ACCESS.NETWORK.WIFISTATE
四.改变WIFI网卡的状态
1.对WIFI网卡进行操作需要通过WIFIManager对象来进行,获取该对象的方法如下:
WIFIManager wifiManager = (wifiManager)Content.getSystemService(Service.WIFI_Service);
2.打开WIFI网卡
wifiManager.setwifiEnabled(true);
3.关闭WIFI网卡
wifiManager.setwifiEnabled(false);
4.获取网卡的状态
wifiManager.getwifistate();
五.注意
1.只有加入权限才能访问网络,否则会暴露权限不够
2.implements是一个内部内
3.Activity是Context的一个子类
<来源网址:http://www.android100.org/html/201406/10/23740.html>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.entel.research"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true">
<activity
android:label="@string/app_name"
android:name=".WifiManagerActivity" >
<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_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.entel.research"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true">
<activity
android:label="@string/app_name"
android:name=".WifiManagerActivity" >
<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_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/wifiManager_open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="打开WiFi" />
<Button
android:id="@+id/wifiManager_close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭WiFi" />
<Button
android:id="@+id/wifiManager_check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示WiFi状态" />
<Button
android:id="@+id/wifiManager_WIFI_SETTINGS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WiFi网络设置" />
<Button
android:id="@+id/threeGManager_State"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示3G边境状态" />
<Button
android:id="@+id/wifiManager_WIRELESS_SETTINGS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="无线网络配置" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/wifiManager_open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="打开WiFi" />
<Button
android:id="@+id/wifiManager_close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭WiFi" />
www.2cto.com
<Button
android:id="@+id/wifiManager_check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示WiFi状态" />
<Button
android:id="@+id/wifiManager_WIFI_SETTINGS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="WiFi网络设置" />
<Button
android:id="@+id/threeGManager_State"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示3G边境状态" />
<Button
android:id="@+id/wifiManager_WIRELESS_SETTINGS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="无线网络配置" />
</LinearLayout>
package com.entel.research;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class WifiManagerActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button wifiManager_open = (Button) findViewById(R.id.wifiManager_open);
Button wifiManager_close = (Button) findViewById(R.id.wifiManager_close);
Button wifiManager_check = (Button) findViewById(R.id.wifiManager_check);
Button wifiManager_WIFI_SETTINGS = (Button) findViewById(R.id.wifiManager_WIFI_SETTINGS);
Button wifiManager_WIRELESS_SETTINGS = (Button) findViewById(R.id.wifiManager_WIRELESS_SETTINGS);
Button threeGManager_State = (Button) findViewById(R.id.threeGManager_State);
final WifiManager wifiManager = (WifiManager) WifiManagerActivity.this
.getSystemService(Context.WIFI_SERVICE);
final ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
wifiManager_open.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
wifiManager.setWifiEnabled(true);
Toast.makeText(WifiManagerActivity.this, "Wifi开启",
Toast.LENGTH_SHORT).show();
}
});
wifiManager_close.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
Toast.makeText(WifiManagerActivity.this, "Wifi关闭",
Toast.LENGTH_SHORT).show();
}
Toast.makeText(WifiManagerActivity.this, "Wifi关闭",
Toast.LENGTH_SHORT).show();
}
});
wifiManager_check.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String result = null;
switch (wifiManager.getWifiState())
{
case WifiManager.WIFI_STATE_DISABLED:
result = "WIFI已关闭";
break;
case WifiManager.WIFI_STATE_DISABLING:
result = "WIFI正在关闭中";
break;
case WifiManager.WIFI_STATE_ENABLED:
result = "WIFI已启用";
break;
case WifiManager.WIFI_STATE_ENABLING:
result = "WIFI正在启动中";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
result = "未知WIFI状态";
break;
}
Toast.makeText(WifiManagerActivity.this, result, Toast.LENGTH_SHORT)
.show();
}
});
wifiManager_WIFI_SETTINGS.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
threeGManager_State.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
State mobile = conMan.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).getState();
Toast.makeText(WifiManagerActivity.this, mobile.toString(),
Toast.LENGTH_SHORT).show();
}
});
wifiManager_WIRELESS_SETTINGS.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
}
}