1. Overall analysis
1.1. The source code is as follows, you can copy directly.
Public classNetutil {/*** The user is connected to the network * *@paramContext Context*/ Public Static Booleanisnetconnection (Context context) {FinalConnectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); FinalNetworkinfo Networkinfo =Connectivitymanager.getactivenetworkinfo (); return(Networkinfo! =NULL&&networkinfo.isavailable ()); } /*** Whether to connect to WiFi * *@paramContext Context*/ Public Static Booleaniswificonnection (Context context) {Connectivitymanager Connectivitymanager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo Wifinetworkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); if(wifinetworkinfo.isconnected ()) {return true; } return false; }}
View Code
1.2. Main methods.
- Determine if your phone is connected to the network
- Determine if the phone is connected to WiFi
There are many other methods that you can use to add again.
2. Local analysis
2.1. Determine if the phone is connected to the network
Here's a system service, Context.getsystemservice (context.connectivity_service) Get manager
Then use an instance of the manager. Getactivenetworkinfo () to get network connection status information.
2.2. Determine if your phone is connected to WiFi
Judging WiFi, in fact, is based on the former, with the manager. Getnetworkinfo (the status code of WiFi is inside)
In fact, the general situation to determine whether the network exists or not, because the network includes mobile phone data connection +wifi.
Android Network General class Netutil