First, determine whether the network type is WiFi, or 3G, or 2G network
Connectivitymanager connectmgr = (connectivitymanager) this.getsystemservice (Context.connectivity_service);
networkinfo info = connectmgr.getactivenetworkinfo (); First, determine whether the network is WiFi, Be sure to make a non-null judgment before judging if there is no network connection Info ==nullinfo.gettype () == ConnectivityManager.TYPE_WIFI II, Determine if the mobile network Info !=null && info.gettype () == connectivitymanager.type_ mobile cell phone Network for detailed distinction: info.getsubtype () Here use getsubtype (), not gettype (), GetType () returned is 0, Or 1, is the cell phone network or wifi info.getsubtype () value list as follows: * NETWORK_TYPE_CDMA Network type is Cdma * network_ type_edge Network type is edge * network_type_evdo_0 Network type is evdo0 * network_type_evdo_a network type is EVDOA * NETWORK_TYPE_GPRS Network type is gprs * network_type_hsdpa Network type is Hsdpa * network_ type_hspa Network type is hspa * network_type_hsupa Network type is hsupa * network_type_umts network type is UMTS
Second, monitoring network status
The following code can monitor whether the network is connected, can do the corresponding operation according to the network connection;
1 Importcom.qdsj.hxcmjj.activity.MainActivity;2 3 ImportAndroid.app.Service;4 ImportAndroid.content.BroadcastReceiver;5 ImportAndroid.content.Context;6 Importandroid.content.Intent;7 ImportAndroid.content.IntentFilter;8 ImportAndroid.net.ConnectivityManager;9 ImportAndroid.net.NetworkInfo;Ten ImportAndroid.os.IBinder; One A Public classNetmonitorserviceextendsService { - - the - PrivateConnectivitymanager Connectivitymanager; - Privatenetworkinfo info; - + PrivateBroadcastreceiver Mreceiver =NewBroadcastreceiver () { - + @Override A Public voidOnReceive (Context context, Intent Intent) { atString action =intent.getaction (); - if(Action.equals (connectivitymanager.connectivity_action)) { -SYSTEM.OUT.PRINTLN ("Network status has changed"); -Connectivitymanager =(Connectivitymanager) Getsystemservice (context.connectivity_service); -info =Connectivitymanager.getactivenetworkinfo (); - if(Info! =NULL&&info.isavailable ()) { inSYSTEM.OUT.PRINTLN ("Network Connection"); -Context.startactivity (NewIntent (context,mainactivity.class). Addflags (Intent.flag_activity_new_task)); to}Else { +SYSTEM.OUT.PRINTLN ("Network Disconnect"); - } the } * } $ };Panax Notoginseng - the @Override + Publicibinder onbind (Intent Intent) { A return NULL; the } + - @Override $ Public voidonCreate () { $ Super. OnCreate (); -Intentfilter Mfilter =NewIntentfilter (); - mfilter.addaction (connectivitymanager.connectivity_action); the registerreceiver (Mreceiver, mfilter); -System.out.println ("Turn on network monitoring service");Wuyi } the - Wu - @Override About Public voidOnDestroy () { $ Super. OnDestroy (); - Unregisterreceiver (mreceiver); - } - A @Override + Public intOnstartcommand (Intent Intent,intFlagsintStartid) { the return Super. Onstartcommand (Intent, flags, startid); - } $ the the the the}