1. public class WIFIReceiver extends BroadcastReceiver {  
  2.  @Override 
  3.  public void onReceive(Context context, Intent intent) {  
  4.  String action = intent.getAction();  
  5.  //监听WIFI状态变化  
  6. if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {  
  7. WifiManager wifiManager = (WifiManager) context  
  8.  .getSystemService(Context.WIFI_SERVICE);  
  9.  Log.e(“Debug”, “Setting wifistate: ” + wifiManager.getWifiState());  
  10.  } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {  
  11.  ////监听连接状态的变化莫测  
  12. NetworkInfo networkInfo = intent  
  13.  .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);  
  14.  Log.e(“Debug”, “Setting isConnected: ” + networkInfo.isConnected());  
  15.  if (networkInfo.isConnected())  
  16.  Toast.makeText(context, “Connected!”, Toast.LENGTH_LONG).show();  
  17.  }  
  18.  }  
  19.  }