1. Register a broadcast receiver to monitor network status
private void Start ()
{
Intentfilter filter = new Intentfilter ();
Filter.addaction (connectivitymanager.connectivity_action);
Filter.addaction (wifimanager.wifi_state_changed_action);
Filter.addaction (wifimanager.network_state_changed_action);
This.registerreceiver (mybroadcastreceiver, filter);
}
2. Unregister the broadcaster
private void Stop ()
{
This.unregisterreceiver (Mybroadcastreceiver);
}
3. New Listener class Networkconnectchangedreceiver inheritance Broadcastreceiver
public class Networkconnectchangedreceiver extends Broadcastreceiver
{
@Override
public void OnReceive (context context, Intent Intent)
{
This listening WiFi is turned on and off, regardless of the WiFi connection
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals (Intent.getaction ()))
{
int wifistate = Intent.getintextra (wifimanager.extra_wifi_state, 0);
Switch (wifistate)
{
Case wifimanager.wifi_state_enabled:
LOG.E ("Apactivity", "wifi_state_enabled");
Break
Case wifimanager.wifi_state_enabling:
LOG.E ("Apactivity", "wifi_state_enabling");
Break
Case wifimanager.wifi_state_disabled:
LOG.E ("Apactivity", "wifi_state_disabled");
Break
Case wifimanager.wifi_state_disabling:
LOG.E ("Apactivity", "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;
Switch (networkinfo.getstate ())
{
Case CONNECTED:
LOG.E ("Apactivity", "CONNECTED");
Break
Case connecting:
LOG.E ("apactivity", "Connecting");
Break
Case disconnected:
LOG.E ("apactivity", "Disconnected");
Break
Case disconnecting:
LOG.E ("Apactivity", "disconnecting");
Break
Case SUSPENDED:
LOG.E ("Apactivity", "SUSPENDED");
Break
Case UNKNOWN:
LOG.E ("Apactivity", "UNKNOWN");
Break
Default
Break
}
}
}
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 ()))
{
Networkinfo info = Intent.getparcelableextra (connectivitymanager.extra_network_info);
if (info! = null)
{
StringBuilder sb = new StringBuilder ();
Sb.append ("Info.gettypename ():" + info.gettypename () + "\ n");
Sb.append ("Getsubtypename ():" + info.getsubtypename () + "\ n");
Sb.append ("GetState ():" + info.getstate () + "\ n");
Sb.append ("Getdetailedstate ():" + info.getdetailedstate (). Name () + "\ n");
Sb.append ("Getdetailedstate ():" + info.getextrainfo () + "\ n");
Sb.append ("GetType ():" + Info.gettype ());
LOG.E ("Apactivity", sb.tostring ());
}
}
}
}
When the network connection is switched on, the WiFi or 3G network is used. Using this article, it is possible to record when to use WiFi and when to use a 3G network.
public void OnReceive (context context, Intent Intent) {
String action = Intent.getaction ();
if (ConnectivityManager.CONNECTIVITY_ACTION.equals (ACTION)) {
Networkinfo ni = Intent.getparcelableextra (connectivitymanager.extra_network_info);
if (ni.getstate () = = state.connected && ni.gettype () = = Connectivitymanager.type_wifi) {
LOG.V ("Kevin", "WiFi Connected");
} else if (ni.getstate () = = state.disconnected && ni.gettype () = = Connectivitymanager.type_wifi) {
LOG.V ("Kevin", "WiFi Disconnected");
}
}
}
}
android-Monitoring network status