android监听WIFI开启与关闭

public class WIFIReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//监听WIFI状态变化
if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
Log.e(“Debug”, “Setting wifistate: ” + wifiManager.getWifiState());
} else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
////监听连接状态的变化莫测
NetworkInfo networkInfo = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
Log.e(“Debug”, “Setting isConnected: ” + networkInfo.isConnected());
if (networkInfo.isConnected())
Toast.makeText(context, “Connected!”, Toast.LENGTH_LONG).show();
}
}
}

发表评论