Directory structure:
contents Structure [+]
- Judging the network
- Determine if there is an Internet connection
- Determine if a WiFi network is available
- Determine if the mobile network is available
- Gets the type information for the current network connection
- Listening Network
Obtaining network information requires that the appropriate permissions be added to the Androidmanifest.xml file.
<android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
1. Determine network 1.1 to determine if there is an Internet connection
Public Boolean isnetworkconnected (Context context) {ifnull== Mconnectivitymanager.getactivenetworkinfo (); if NULL ) {return mnetworkinfo.isavailable ();}} return false ;}
1.2 Determining if a WiFi network is available
public boolean iswificonnected (context context) { if (context! = null ) {Connectivitymanager Mconnectivitymanager = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo mwifinetworkinfo = Mconnectivitymanager.getnetworkinfo ( Connectivitymanager.type_wifi); if (Mwifinetworkinfo! = null return Mwifinetworkinfo.isavailable ();}} return false ;}
It is important to note that this method only determines whether WiFi is available in the current environment, not whether it is connected to Wi-Fi.
1.3 Determine if the mobile network is available
public boolean ismobileconnected (context context) { if (context! = null ) {Connectivitymanager Mconnectivitymanager = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo mmobilenetworkinfo = Mconnectivitymanager.getnetworkinfo ( Connectivitymanager.type_mobile); if (Mmobilenetworkinfo! = null return Mmobilenetworkinfo.isavailable ();}} return false ;}
1.4 Get the type information for the current network connection
public static int Getconnectedtype (context context) { if (Context! = null = (Connectivitymanager) Context.getsystemservice ( Context.connectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (Mnetworkinfo! = null && return Mnetworkinfo.gettype ();}} return -1
The return value of GetType () can be type_mobile, Type_wifi, Type_wimax, Type_ethernet, Type_bluetooth, and other types in Connectivitymanager.
2. Listening Network
Define the broadcast recipient:
Broadcastreceiver Connectionreceiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {Connectivitymanager Mconnectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Mnetworkinfo=Mconnectivitymanager. Getactivenetworkinfo (); intType =-1; if(Mnetworkinfo! =NULL&&mnetworkinfo.isavailable ()) {Type=Mnetworkinfo.gettype (); } if(Type = =Connectivitymanager.type_wifi) {Toast.maketext (mainactivity). This, "WiFi", Toast.length_short). Show (); } Else if(Type = =connectivitymanager.type_mobile) {Toast.maketext (mainactivity). This, "Network Available", Toast.length_short). Show (); }Else if(Type==-1) {Toast.maketext (mainactivity. This, "Network Unavailable", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This, "Unknown Network", Toast.length_short). Show (); } } };
Register at the right place:
New Intentfilter (); Intentfilter.addaction (connectivitymanager.connectivity_action); Registerreceiver (Connectionreceiver, intentfilter);
To cancel the registration in the appropriate location:
Unregisterreceiver (Connectionreceiver);
"Android" detailed Android network operation