WiFi状态判断

对WiFi进行设置主要借助WifiManager对象。

//WiFiMangager常用的状态:
// WiFiManager.WIFI_STATE_ENABLED WiFi已打开
// WiFiManager.WIFI_STATE_ENABLING WiFi打开中
// WiFiManager.WIFI_STATE_DISABLING WiFi关闭中
// WiFiManager.WIFI_STATE_DISABLED WiFi已关闭
// WiFiManager.WIFI_STATE_UNKNOWN 未知的WiFi状态
//程序针对上述的状态事件进行逻辑判断,并判断CheckBox上的文字是“打开”还是“关闭”,再配合WiFi当下的状态,将可否顺利打开或关闭WiFi,以TextView文字显示于画面中
//主程序中包括了所有WiFi连接状态的判断,并针对不同状态的结果,决定是否要打开或关闭WiFi服务。在Activity的onCreate事件中,设计了一个CheckBox与其OnClickListener
//并在User单击此CheckBox时,先判断CheckBox的状态(选取或者是取消选取),在选取状态下即打开WiFi服务的事件,取消选取的状态下,及运行关闭WiFi服务的事件
//在打开WiFi的过程,会经过以下顺序:
//1.WiFiManager.WIFI_STATE_DISABLED
//2.WiFiManager.WIFI_STATE_DISABLEING
//3.WiFiManager.WIFI_STATE_ENABLED
//同样关闭WiFi的过程也要经过三个:
//1.WiFiManager.WIFI_STATE_ENABLED
//2.WiFiManager.WIFI_STATE_DISABLING
//3.WiFiManager.WIFI_STATE_DISABLED

源码如下:

package com.allove.wfp;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class WIFIManagerTest extends Activity {
private CheckBox mCheckBox;
private TextView mTextView;
private WifiManager mWifiManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.Init();
//判断WiFi状态是否为打开
if(mWifiManager.isWifiEnabled())
{
if(mWifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED)
{
//若WiFi已打开,将选取项打勾
mCheckBox.setChecked(true);
//更改选取项文字为关闭WiFi
mCheckBox.setText("关闭WiFi");
}
else
{
//若WiFi未打开,将选取项打钩
mCheckBox.setChecked(false);

mCheckBox.setText("打开WiFi");
}
}
else
{
mCheckBox.setChecked(false);
mCheckBox.setText("打开WiFi");
}

mCheckBox.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mCheckBox.isChecked()==false)
{
try
{
//判断WiFi是否打开了
if(mWifiManager.isWifiEnabled())
{
if(mWifiManager.setWifiEnabled(false))
{
mTextView.setText("WiFi已经关闭");
}
else
{
mTextView.setText("WiFi关闭失败");
}
}
else
{
//WiFi状态不为已打开状态时
switch(mWifiManager.getWifiState())
{
case WifiManager.WIFI_STATE_ENABLING:
mTextView.setText("WIFI_STATE_ENABLING");
break;
case WifiManager.WIFI_STATE_DISABLING:
mTextView.setText("WIFI_STATE_DISABLING");
break;
case WifiManager.WIFI_STATE_DISABLED:
mTextView.setText("WIFI_STATE_DISABLED");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
default:
mTextView.setText("WIFI_STATE_UNKNOWN");
break;
}
mCheckBox.setText("打开WiFi");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else if(mCheckBox.isChecked()==true)
{
try
{
if(!mWifiManager.isWifiEnabled()&&mWifiManager.getWifiState()!=WifiManager.WIFI_STATE_ENABLING)
{
if(mWifiManager.setWifiEnabled(true))
{
switch(mWifiManager.getWifiState())
{
case WifiManager.WIFI_STATE_ENABLING:
mTextView.setText("WIFI_STATE_ENABLING");
break;
case WifiManager.WIFI_STATE_ENABLED:
mTextView.setText("WIFI_STATE_ENABLED");
break;
default:
mTextView.setText("WIFI_STATE_UNKNOWN");
break;
}
}
else
{
switch(mWifiManager.getWifiState())
{
case WifiManager.WIFI_STATE_ENABLING:
mTextView.setText("WIFI_STATE_ENABLING");
break;
case WifiManager.WIFI_STATE_DISABLING:
mTextView.setText("WIFI_STATE_DISABLING");
break;
case WifiManager.WIFI_STATE_DISABLED:
mTextView.setText("WIFI_STATE_DISABLED");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
default:
mTextView.setText("WIFI_STATE_UNKNOWN");
break;
}
}
mCheckBox.setText("关闭WiFi");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
}
public void Init()
{
mCheckBox=(CheckBox)this.findViewById(R.id.CheckBox01);
mTextView=(TextView)this.findViewById(R.id.TextView01);

//获得WifiManager对象
mWifiManager=(WifiManager)this.getSystemService(Context.WIFI_SERVICE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
try
{
switch(mWifiManager.getWifiState())
{
case WifiManager.WIFI_STATE_ENABLED:
mTextView.setText("WIFI_STATE_ENABLED");
break;
case WifiManager.WIFI_STATE_ENABLING:
mTextView.setText("WIFI_STATE_ENABLING");
break;
case WifiManager.WIFI_STATE_DISABLED:
mTextView.setText("WIFI_STATE_DISABLED");
break;
case WifiManager.WIFI_STATE_DISABLING:
mTextView.setText("WIFI_STATE_DISABLING");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
default:
mTextView.setText("WIFI_STATE_UNKNOWN");
break;
}
}
catch(Exception e)
{
e.printStackTrace();
}
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}

注意要添加权限:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.allove.wfp" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WIFIManagerTest" 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="3" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
</manifest>

你可能感兴趣的:(.net,android,xml,OS,Access)