Android app Development in most cases to connect with the network, how to determine whether the network is connected, what kind of connection? Here is a way to write about the connection, you can make the appropriate changes according to the requirements:
Private String Connection (context context) {
Determine the network, be sure to remember write permission <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></ Uses-permission>
String connectinfo = "Network Not Connected";
Connectivitymanager connect = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo info = Connect.getactivenetworkinfo ();
if (info! = null && info.isconnected ()) {
if (info.isavailable ()) {//This judgment is used with an exception and needs to be handled
Connectinfo = "Network is connected,";
Whether the GPS is turned on
if (isgpsenabled (this)) {
Connectinfo + = "GPS has been turned on";
}else {
Connectinfo + = "GPs not turned on";
}
Does WiFi Open
if (iswifienabled (This,connect)) {
Connectinfo + = "WiFi on";
}else {
Connectinfo + = "WiFi not turned on";
}
int netType = Info.gettype ();
if (NetType = = Connectivitymanager.type_wifi) {
Connectinfo + = "\ n currently using WiFi";
}else if (NetType = = Connectivitymanager.type_mobile) {
Connectinfo + = "\ n currently using mobile connection";
}else {
Connectinfo + = "\ n currently used connection is:" +nettype;
}
}
return connectinfo;
}
Determine if the GPS is open
public static Boolean isgpsenabled (context context) {
Locationmanager lm = ((Locationmanager) context
. Getsystemservice (Context.location_service));
list<string> accessibleproviders = Lm.getproviders (true);
return accessibleproviders! = null && accessibleproviders.size () > 0;
}
Determine if WiFi is turned on
public static Boolean iswifienabled (Context Context,connectivitymanager mgrconn) {
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 your Android phone is connected