1.WIFI_STATE_DISABLED:WIFI网卡不可用
2.WIFI_STATE_DISABLING:WIFI网卡正在关闭
1.WIFI_STATE_EWNABLED:WIFI网卡可用
1.WIFI_STATE_ENABLING:WIFI网卡正在打开
1.WIFI_STATE_UNKNOWN:未知网卡状态
改变WIFI网卡的状态
对WIFI网卡进行操作需要通过WifiManager对象进行,获取该对象的方法如下:
WifiManger wifiManger = (WifiManger)Context.getSystemService(Service.WIFI_SERVICE); //得到系统服务
打开WIFI网卡
wifiManger.setWifiEnabled(true);
打开WIFI网卡
wifiManger.setWifiEnabled(false);
获取网卡当前状态
wifiManger.getWifiState();
android在操作wifi之前需要给app赋予权限:
在AndroidManifest.xml文件中加入红色的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yx.wificontrol"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.yx.wificontrol.WifiControlActivity"
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>
<!-- 添加wifi网络需要的权限 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
</manifest>
代码比较简单,WifiControlActivity.java文件
package com.yx.wificontrol;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class WifiControlActivity extends Activity {
private Button startWifi;
private Button stopWifi;
private Button seeWifi;
private WifiManager wifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_control);
startWifi = (Button) findViewById(R.id.startWifi);
stopWifi = (Button) findViewById(R.id.stopWifi);
seeWifi = (Button) findViewById(R.id.seeWifi);
startWifi.setOnClickListener(new StartWifiListener());
stopWifi.setOnClickListener(new StopWifiListener());
seeWifi.setOnClickListener(new SeeWifiListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.wifi_control, menu);
return true;
}
class StartWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
System.out.println("StartWifiListener");
//Toast.makeText(WifiControlActivity.this, "当前网卡状态:"+wifiManager.getWifiState(), Toast.LENGTH_LONG);
wifiManager = (WifiManager) WifiControlActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
System.out.println("wifi 状态:"+wifiManager.getWifiState());
Toast.makeText(WifiControlActivity.this, "当前网卡状态:"+wifiManager.getWifiState(), Toast.LENGTH_LONG).show();
}
}
class StopWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
System.out.println("StopWifiListener");
//Toast.makeText(WifiControlActivity.this, "当前网卡状态:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT);
wifiManager = (WifiManager) WifiControlActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
System.out.println("wifi 状态:"+wifiManager.getWifiState());
Toast.makeText(WifiControlActivity.this, "当前网卡状态:"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
}
class SeeWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
System.out.println("SeeWifiListener");
wifiManager = (WifiManager) WifiControlActivity.this.getSystemService(Context.WIFI_SERVICE);
System.out.println("wifi 状态:"+wifiManager.getWifiState());
Toast.makeText(WifiControlActivity.this, "当前网卡状态:"+wifiManager.getWifiState(), Toast.LENGTH_LONG).show();
}
}
}
顺便贴出布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".WifiControlActivity" >
<Button
android:id="@+id/startWifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动wifi网卡" />
<Button
android:id="@+id/stopWifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/startWifi"
android:text="关闭wifi网卡" />
<Button
android:id="@+id/seeWifi"
android:layout_below="@id/stopWifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看wifi网卡状态" />
</RelativeLayout>