Detect network changes (WiFi, 2g, 3g, 4g) 1. Register the broadcast "Android.net.conn.CONNECTIVITY_CHANGE" and "Android.net.wifi.STATE_CHANGE" for monitoring:
Private class Connectivitybroadcastreceiver extends Broadcastreceiver {@Override public void onreceive (Cont Ext context, Intent Intent) {try {String action = intent.getaction (); LOG.E ("Connectivitybroadcastreceiver", "action=" +action); if (!action.equals (connectivitymanager.connectivity_action) &&!action.equals (Wifimanager . network_state_changed_action)) | | !islistening_) {return; } if (Intent.getaction (). Equals (Wifimanager.network_state_changed_action)) {Networkinfo _ = Intent.getparcelableextra (Wifimanager.extra_network_info); if (networkinfo_.isconnected ()) {//WIFI is connected State_ = Net_state_w ifi_connected; }else{state_ = net_state_disconnected; }} else if (Intent.getaction (). Equals (connectivitymanager.connectivity_action)) {net Workinfo_ = (networkinfo) Intent.getparcelableextra (connectivitymanager.extra_network_info); String APN = Networkinfo_.getextrainfo (); LOG.E ("Connectivitybroadcastreceiver", "apn=" +APN); Boolean noconnectivity = Intent.getbooleanextra (connectivitymanager.extra_no_connectivity, false); if (networkinfo_.gettype () = = Connectivitymanager.type_wifi) {if (networkinfo_.isconnected ()) { State_ = net_state_wifi_connected; }else{State_ = noconnectivity? net_state_disconnected:net_state_wifi_connected; }}else if (networkinfo_.gettype () = = Connectivitymanager.type_mobile) {if (Nocon nectivity) {State_ = net_state_disconnected; } else {Telephonymanager Telephonymanager = (telephonymanager) context.getsystemservice (Contex T.telephony_service); int type = Telephonymanager.getnetworktype (); LOG.E ("Connectivitybroadcastreceiver", "type=" +type); State_ = Getnetworkclass (type); }}else {if (noconnectivity) {State_ = Net_state_di sconnected; } else {state_ = Net_state_unknown; }}} notifyevent (); } catch (Exception e) {e.printstacktrace (); } } };
2. Determine the type of network under Type_mobile:
private int Getnetworkclass (int networktype) {switch (networktype) {case Telephonymanager.network_type _gprs:case TelephonyManager.NETWORK_TYPE_EDGE:case TelephonyManager.NETWORK_TYPE_CDMA:case T ElephonyManager.NETWORK_TYPE_1xRTT:case TelephonyManager.NETWORK_TYPE_IDEN:return Net_state_2g_con nected; Case TelephonyManager.NETWORK_TYPE_UMTS:case TelephonyManager.NETWORK_TYPE_EVDO_0:case Telephonymanage R.network_type_evdo_a:case TelephonyManager.NETWORK_TYPE_HSDPA:case Telephonymanager.network_type_hsup A:case TelephonyManager.NETWORK_TYPE_HSPA:case TelephonyManager.NETWORK_TYPE_EVDO_B:case Tel EphonyManager.NETWORK_TYPE_EHRPD:case TelephonyManager.NETWORK_TYPE_HSPAP:return Net_state_3g_conn ected; Case TelephonyManager.NETWORK_TYPE_LTE:return net_state_4g_connected; Default return net_state_unknown; } }
3. Add Permissions in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
Detect network changes (WiFi, 2g, 3g, 4g)