public class Networkprober {
/**
* Whether the network is available
*
* @param activity
* @return
*/
public static Boolean isnetworkavailable (context context) {
Connectivitymanager connectivity = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
if (connectivity = = NULL) {
} else {
Networkinfo[] info = Connectivity.getallnetworkinfo ();
if (info! = null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getstate () = = NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
/**
* Whether the GPS is turned on
*
* @param context
* @return
*/
public static Boolean isgpsenabled (context context) {
Locationmanager Locationmanager = ((Locationmanager) context
. Getsystemservice (Context.location_service));
list<string> accessibleproviders = Locationmanager.getproviders (true);
return accessibleproviders! = null && accessibleproviders.size () > 0;
}
/**
* WiFi is turned on
*/
public static Boolean iswifienabled (context context) {
Connectivitymanager mgrconn = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Telephonymanager Mgrtel = (telephonymanager) context
. Getsystemservice (Context.telephony_service);
Return ((Mgrconn.getactivenetworkinfo ()! = null && mgrconn
. Getactivenetworkinfo (). GetState () = = NetworkInfo.State.CONNECTED) | | Mgrtel
. Getnetworktype () = = Telephonymanager.network_type_umts);
}
/**
* Determine if the current network is a WiFi network
* IF (Activenetinfo.gettype () ==connectivitymanager.type_mobile) {//Judgment 3G Net
*
* @param context
* @return Boolean
*/
public static Boolean Iswifi (context context) {
Connectivitymanager Connectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo activenetinfo = Connectivitymanager.getactivenetworkinfo ();
if (activenetinfo! = null
&& Activenetinfo.gettype () = = Connectivitymanager.type_wifi) {
return true;
}
return false;
}
/**
* Determine if the current network is a 3G network
*
* @param context
* @return Boolean
*/
public static Boolean is3g (context context) {
Connectivitymanager Connectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo activenetinfo = Connectivitymanager.getactivenetworkinfo ();
if (activenetinfo! = null
&& Activenetinfo.gettype () = = Connectivitymanager.type_mobile) {
return true;
}
return false;
}
}
There are also two ways to determine if a network is available:
public static Boolean isnetworkavailable_00 (context context) {
Connectivitymanager cm = ((Connectivitymanager) context
. Getsystemservice (Context.connectivity_service));
if (cm! = null) {
Networkinfo info = Cm.getactivenetworkinfo ();
if (info! = null && info.isconnectedorconnecting ()) {
return true;
}
}
return false;
}
public static Boolean isnetworkavailable_01 (context context) {
Connectivitymanager cm = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo network = Cm.getactivenetworkinfo ();
if (network! = null) {
return network.isavailable ();
}
return false;
}
A more rigorous notation:
public static Boolean checknet (context context) {
try {
Connectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);
if (connectivity! = NULL) {
Networkinfo info = Connectivity.getactivenetworkinfo ();
if (info! = null && info.isconnected ()) {
if (info.getstate () = = NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
return false;
}
return false;
}
Network:android Network Judgment (WiFi, 3G and others)