Import Android.content.context;import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.net.uri;import Android.net.networkinfo.state;import Android.telephony.telephonymanager;import Android.util.log;public class Networkhelper {public static final int networn_none = 0; public static final int networn_wifi = 1; public static final int networn_mobile = 2;private static String Log_tag = "Networkhelper";p ublic static uri uri = Uri.par SE ("content://telephony/carriers");/** * Infer if there is a network */public static Boolean isnetworkavailable (context context) { Connectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); (connectivity = = NULL) {LOG.W (Log_tag, "couldn ' t get Connectivity Manager");} else {networkinfo[] info = connectivity.getallnetworkinfo (); if (info! = null) {for (int i = 0; i < info.length; i++) {i F (info[i].isavailable ()) {LOG.D (Log_tag, "Network is available"); return true;}}} LOG.D (Log_tag, "networK is not available "); return false;} /** * Check network status * @param context * @return */public static Boolean checknetstate (Context context) {Boolean netstate = Fals E Connectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); (Connectivity! = NULL) {networkinfo[] info = connectivity.getallnetworkinfo (); if (info! = null) {for (int i = 0; i < info.length; i++) {if (INF O[i].getstate () = = NetworkInfo.State.CONNECTED) {netstate = True;break;}}}} return netstate; }/** * Infer If the network is roaming */public static Boolean isnetworkroaming (context context) {Connectivitymanager connectivity = (Connectiv Itymanager) Context.getsystemservice (Context.connectivity_service), if (CONNECTIVITY = = null) {LOG.W (Log_tag, "couldn ' t get Connectivity Manager ');} else {Networkinfo info = connectivity.getactivenetworkinfo (); if (info! = null&& Info.gettype () = = Connectivitymanager.type_mobile) {Telephonymanager TM = (Telephonymanager) context.getsystemservice (Context.TELEPHOny_service); if (tm! = null && tm.isnetworkroaming ()) {LOG.D (Log_tag, "Network is roaming"); return true;} else {Lo G.D (Log_tag, "Network is not Roaming");}} else {log.d (Log_tag, "not using mobile network");}} return false;} /** * Infer if mobile network is available * * @param context * @return * @throws Exception */public Static Boolean ismobiledataenable (context Context) {Connectivitymanager Connectivitymanager = (connectivitymanager) context.getsystemservice ( Context.connectivity_service); Boolean ismobiledataenable = False;ismobiledataenable = Connectivitymanager.getnetworkinfo (Connectivitymanager.type_mobile). isconnectedorconnecting (); return ismobiledataenable;} /** * Infer if WiFi is available * @param context * @return * @throws Exception */public Static Boolean iswifidataenable (context context) {Connectivitymanager Connectivitymanager = (connectivitymanager) context.getsystemservice (Context.CONNECTIVITY_ SERVICE); Boolean iswifidataenable = False;iswifidataenable = Connectivitymanager.getnetworkinfo (connectivitymAnager. Type_wifi). isconnectedorconnecting (); return iswifidataenable;} public static int Getnetworkstate (context context) {Connectivitymanager Connmanager = (connectivitymanager) Context . Getsystemservice (Context.connectivity_service); Wifi state state = Connmanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if (state = = State.connected| | state = = state.connecting) {return networn_wifi; }//3g state = Connmanager.getnetworkinfo (connectivitymanager.type_mobile). GetState (); if (state = = State.connected| | state = = state.connecting) {return networn_mobile; } return Networn_none; }}
Android Tools Class (2) Networkhelper Network tool class