android wifi状态更新

private String obtainWifiInfo ( Context context )
	{
		// Wifi的连接速度及信号强度:  
		WifiManager wifiManager = ( WifiManager ) context
				.getSystemService ( WIFI_SERVICE );
		// WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
		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 ();

			wifiSsidTv.setText ( ssid );
			wifiSpeedTv.setText ( String.valueOf ( speed ) );
			wifiUnitsTv.setText ( units );
			wifiStrengthTv.setText ( String.valueOf ( strength ) );
		}
		return info.toString ();
	}

	public BroadcastReceiver	rssiReceiver	= new BroadcastReceiver ()
												{

													@Override
													public void onReceive (
															Context context ,
															Intent intent )
													{
														obtainWifiInfo ( context );
													}
												};

	@Override
	public void onResume ()
	{
		super.onResume ();
		registerReceiver ( rssiReceiver , new IntentFilter (
				WifiManager.RSSI_CHANGED_ACTION ) );

	}

	@Override
	public void onPause ()
	{
		super.onPause ();
		unregisterReceiver ( rssiReceiver );
	}

你可能感兴趣的:(android)