This article for everyone to share the Android network connection judgment and related processing, for your reference, the specific contents are as follows
Obtaining network information requires the appropriate permissions to be added to the Androidmanifest.xml file.
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
1) To determine whether there is a network connection
Public boolean isnetworkconnected {
if (context!= null) {
Connectivitymanager Mconnectivitymanager = (connectivitymanager) context
. Getsystemservice (context.connectivity_service);
Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();
if (mnetworkinfo!= null) {return
mnetworkinfo.isavailable ();
}
}
return false;
2 Determine if WiFi network is available
Public boolean iswificonnected {
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;
}
3 Determine if mobile network is available
Public boolean ismobileconnected {
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;
4) Get the type information of the current network connection
public static int Getconnectedtype {
if (context!= null) {
Connectivitymanager Mconnectivitymanager = (connectivitymanager) context
. Getsystemservice (context.connectivity_service);
Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();
if (mnetworkinfo!= null && mnetworkinfo.isavailable ()) {return
mnetworkinfo.gettype ();
}
}
return-1;
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.