Sometimes our application may need to determine if the current device is networked
1 Private voidinit () {2 /**get system-level networking administrator Objects*/3Connectivitymanager manager = (Connectivitymanager) This4 . Getsystemservice (connectivity_service);5Networkinfo info =manager.getactivenetworkinfo ();6 if(Info = =NULL) {//without the network condition7 //jump to the Network Settings page8StartActivity (NewIntent (Android.provider.Settings.ACTION_SETTINGS));9}Else{//In the case of a networkTen if(Info.isavailable ()) {//when the network is available One /**It's the way the phone comes online.*/ A if(Info.gettype () = =connectivitymanager.type_mobile) { - if(info.getstate () = =state.connected) { - Toast.maketext ( theMainactivity. This, -"Mobile network is available and connected" + "\ n" + "connect network as: \ n" -+ info.gettype () + ", MOBILE", 1). Show (); - } + /**WiFi connection Mode*/ -}Else { +Toast.maketext (mainactivity. This, A"Connection Network mode:" + info.gettype () + ", Wi-Fi", 1). Show (); atStartActivity (NewIntent ( - Android.provider.Settings.ACTION_WIFI_SETTINGS)); - } -}Else { -Toast.maketext (mainactivity. This, "Mobile phone network unavailable", 1). Show (); - } in } -}
However, finally note that the following permissions are added to allow the program to access information about GSM network (allows applications to accessinformation about networks), otherwise it will be an error
<android:name= "Android.permission.ACCESS_NETWORK_STATE"/>