Android Wifi开关,信号,状态改变监听

在做的项目中需要显示一个WIFI状态的图标,与系统通知栏里的类似,要求动态显示WIFI信号,状态.
Wifi在信号改变,开关以及状态改变时都会发送广播,我们接收这几个广播即可.
需要权限:


    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Wifi图标这里,我用了一个ImageView,使用level-list来在信号改变时切换显示的图片:


<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:maxLevel="0"
        android:drawable="@drawable/stateitems_wifi_0"
        android:minLevel="0"/>
    <item
        android:drawable="@drawable/stateitems_wifi_1"
        android:maxLevel="1"
        android:minLevel="1"/>
    <item
        android:drawable="@drawable/stateitems_wifi_2"
        android:maxLevel="2"
        android:minLevel="2"/>
    <item
        android:drawable="@drawable/stateitems_wifi_3"
        android:maxLevel="3"
        android:minLevel="3"/>
    <item
        android:drawable="@drawable/stateitems_wifi_4"
        android:maxLevel="4"
        android:minLevel="4"/>

</level-list>

但好像有个问题,在返回信号3时,显示的是4,满格信号,2的时候,显示的3格信号不知道问题出在哪,我确定图片没有弄错.

广播接收器:


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.ImageView;

public class WifiStateReceiver extends BroadcastReceiver {

	ImageView wifiStateImage;
	Context context;
	public WifiStateReceiver(Context context, ImageView imageView) {
		// TODO Auto-generated constructor stub
		this.wifiStateImage=imageView;
		this.context=context;
		int strength=getStrength(context);
		wifiStateImage.setImageLevel(strength);
		
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println(intent.getAction());
		if(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION))
		{
			int strength=getStrength(context);
			System.out.println("当前信号 "+strength);
			wifiStateImage.setImageLevel(strength);
		}else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
			System.out.println("网络状态改变");
			NetworkInfo info=intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
			if(info.getState().equals(NetworkInfo.State.DISCONNECTED))
			{//如果断开连接
				wifiStateImage.setImageLevel(0);
			}
		}else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
		{
			//WIFI开关
			int wifistate=intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,WifiManager.WIFI_STATE_DISABLED);
			if(wifistate==WifiManager.WIFI_STATE_DISABLED)
			{//如果关闭
				wifiStateImage.setImageLevel(0);
			}
		}

	}
	public int getStrength(Context context)
	{
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifiManager.getConnectionInfo();
		if (info.getBSSID() != null) {
			int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
			// 链接速度
//			int speed = info.getLinkSpeed();
//			// 链接速度单位
//			String units = WifiInfo.LINK_SPEED_UNITS;
//			// Wifi源名称
//			String ssid = info.getSSID();
			return strength;
			
		}
		return 0;
	}

}

注册广播接收器:


		//WIFI状态接收器
		WifiStateReceiver wifiReceiver=new WifiStateReceiver(this,wifi_state);
		IntentFilter filter=new IntentFilter();
		filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
		filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
		filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		this.registerReceiver(wifiReceiver,filter);

要加上三个Action

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记