1 system broadcast (shutdown, lock and, voice, phone, signal, etc system-provided broadcast) (e.g. signal)
Broadcasts are typically written directly in a class without having to write them out in a single class, which facilitates the processing of data when the result is heard
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;
Import android.net.NetworkInfo.State;
Import Android.net.wifi.WifiManager;
Import android.os.Parcelable;
Import Android.util.Log;
public class Networkconnectchangedreceiver extends Broadcastreceiver {
/***
* 1 Code Register broadcast
* intentfilter filter = new Intentfilter ();
filter.addaction (wifimanager.network_state_changed_action);
filter.addaction (wifimanager.wifi_state_changed_action);
filter.addaction (connectivitymanager.connectivity_action);
registerreceiver (New Networkconnectchangedreceiver (), filter);
2 Configuring in the checklist
*/
@Override
public void OnReceive (context context, Intent Intent) {
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals (Intent.getaction ())) {//This listening WiFi is turned on and off, regardless of the WiFi connection
int wifistate = Intent.getintextra (wifimanager.extra_wifi_state, 0);
LOG.E ("h3c", "wifistate" + wifistate);
Switch (wifistate) {
Case wifimanager.wifi_state_disabled:
Break
Case wifimanager.wifi_state_disabling:
Break
//
}
}
The connection status of the listening WiFi is connected to a valid no-line, when the state of the broadcast above is wifimanager.wifi_state_disabling, and wifi_state_disabled, it will not receive this broadcast.
Broadcast on the top of the broadcast is the wifimanager.wifi_state_enabled state will also receive this broadcast, of course, just open WiFi must not be connected to a valid wireless
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals (Intent.getaction ())) {
Parcelable Parcelableextra = Intent.getparcelableextra (Wifimanager.extra_network_info);
if (null! = Parcelableextra) {
Networkinfo Networkinfo = (networkinfo) Parcelableextra;
state state = Networkinfo.getstate ();
Boolean isconnected = state = = state.connected;//Of course, this way you can determine the status more precisely.
LOG.E ("h3c", "isconnected" + isconnected);
if (isconnected) {
} else {
}
}
}
This listens for network connection settings, including WiFi and mobile data opening and closing.
It's better to use this monitor. WiFi is monitored if it is turned on, closed, and the connection is available on the connection. See log
The biggest drawback of this broadcast is that it is slower than the top two broadcasts, and if you just want to listen to WiFi, I think it's more appropriate to use the top two mates.
if (ConnectivityManager.CONNECTIVITY_ACTION.equals (Intent.getaction ())) {
Connectivitymanager manager = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);
Networkinfo GPRS = Manager.getnetworkinfo (connectivitymanager.type_mobile);
Networkinfo WiFi = manager.getnetworkinfo (Connectivitymanager.type_wifi);
LOG.I ("Networkconnectchangedreceiver", "Network status change:" + wifi.isconnected () + "3g:" + gprs.isconnected ());
Networkinfo info = Intent.getparcelableextra (connectivitymanager.extra_network_info);
if (info! = null) {
LOG.E ("h3c", "Info.gettypename ()" + info.gettypename ());
LOG.E ("h3c", "Getsubtypename ()" + info.getsubtypename ());
LOG.E ("h3c", "getState ()" + info.getstate ());
LOG.E ("h3c", "getdetailedstate ()" + info.getdetailedstate (). name ());
LOG.E ("h3c", "getdetailedstate ()" + info.getextrainfo ());
LOG.E ("h3c", "GetType ()" + Info.gettype ());
if (NetworkInfo.State.CONNECTED = = Info.getstate ()) {
} else if (info.gettype () = = 1) {
if (NetworkInfo.State.DISCONNECTING = = Info.getstate ()) {
}
}
}
}
}
}
2 Custom broadcasts (broadcast based on demand)
Pay attention to sending the broadcast and receiving the docking of the broadcast
Broadcast in Android