:
Tool class
/*** Get Network connection Information * * According to Networkinfo can know there are a lot of connection methods and information * *① when there is no available network, Networkinfo is null to determine whether Networkinfo is null* *② When only WiFi network or WiFi network and mobile network exist simultaneously, return WiFi network connection information * * Networkinfo parameter is as follows: * * detailedstate:connected (Connection status) * * ExtraInfo: YITENG1 (WiFi Network Name) * * ISAVAIABLE:TRUE (available) * * networktype:1* * state:connected* * subtype:0* * TYPENAME:WIFI (network type name) * *③ When only the mobile network, return the mobile network connection information * * Networkinfo parameters are as follows: * * detailedstate:connected (Connection status) * * extrainfo:cmnet (mobile network Name) * * Isavaiable: true* * networktype:0* * state:connected* * subtype:2* * typename:mobile (network type name) * *@paramcontext*@return*/ Public StaticNetworkinfo getactivenetwork (context context) {if(Context = =NULL) {return NULL;} Connectivitymanager Mconnmgr=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); if(Mconnmgr = =NULL) {return NULL;} //Get Active network connection informationNetworkinfo Aactiveinfo=Mconnmgr.getactivenetworkinfo (); returnAactiveinfo;}
How to use: on a page that needs to listen to the network
Networkinfo Networkinfo = Getsysteminfo.getactivenetwork ( This); if(Networkinfo = =NULL) {Toastutils.showcustomtoastlong ("No network connection available, check your network settings"); } Else { if(networkinfo.isavailable () = =false) {Toastutils.showcustomtoastlong ("No network connection available, check your network settings"); } Else{String Networktype=Networkinfo.gettypename (); if(AppConstants.NETWORK_WIFI.equalsIgnoreCase (Networktype)) {Toastutils.showcustomtoastlong ("Connected to WiFi network" +networkinfo.getextrainfo ()); } Else if(AppConstants.NETWORK_MOBILE.equalsIgnoreCase (Networktype)) {Toastutils.showcustomtoastlong ("You are currently using a mobile network"); } } }
Android Get network connection information