Detect network changes (WiFi, 2g, 3g, 4g)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.