public class Netutil {/** * checks the current network */public static Boolean checknet (context context) {//Determines whether the current user's phone is using WiFi traffic boolean Iswifi = iswificonnection (context);//Determines whether the current user's phone uses APN communication Boolean ISAPN = Isapnconnection (context);/no--Prompts the user--interface jumps to network settings if ( ISAPN = = False && Iswifi = = False) {return false;} if (ISAPN) {//is the access point//IP is the 10.0.0.172 port is 80 read from the phone's configuration information//a part of the phone: 010.000.000.172setAPN (context);} return true;} /** * Read */private static void Setapn (context context) {Uri Preferred_apn_uri = Uri.parse from the configuration information of the phone ("content://telephony/c ARRIERS/PREFERAPN "); Contentresolver contentresolver = Context.getcontentresolver (); Cursor query = contentresolver.query (Preferred_apn_uri, NULL, NULL, NULL),//Gets the APN information that is currently being connected if (query!=null& &query.movetonext ()) {//Set IP and port information globalparams.ip=query.getstring (Query.getcolumnindex ("proxy")); Globalparams.port=query.getint (Query.getcolumnindex ("PORT"));}} /** * Determine if WiFi is connected * * @return */private static Boolean iswificonnection (context context) {Connectivitymanager manaGER = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo networkinfo = Manager.getnetworkinfo (Connectivitymanager.type_wifi); if (networkinfo! = null) return Networkinfo.isconnected (); return false;} /** * Determine if APN is connected * * @return */private static Boolean isapnconnection (context context) {Connectivitymanager manager = (Con Nectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo networkinfo = Manager.getnetworkinfo (connectivitymanager.type_mobile); if (NetworkInfo! = null) return Networkinfo.isconnected (); return false;}}
Android Network Connection Tool class