Android network switch sends multiple broadcast issues

Source: Internet
Author: User

Recently found doing project monitoring network switching broadcasts, switching some settings according to network conditions. The test found that each time 3g-wifi or WIFI to 3G, the network switch broadcast will be issued multiple times. Like 3g-->wifi.

Three broadcasts will be sent 1. Connect WiFi 2. Turn off the phone network 3. Is there any way to determine the process of connecting WiFi? So let's see a class.

Http://www.androidcommunitydocs.com/reference/android/net/ConnectivityManager.html (Official API document)

public class

Connectivitymanager extends Object

Java.lang.Object
? android.net.ConnectivityManager

Class that answers queries on the state of the network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getsystemservice (Context.connectivity_servic E).

The primary responsibilities of this class is to the main duties:

    1. Monitor Network Connections (Wi-Fi, GPRS, UMTS, etc) Monitor Network Connections
    2. Send broadcast intents when network connectivity changes connection changes
    3. Attempt to ' fail over ' to another network time connectivity to a network is lost try to switch to other networks when the current network is unavailable
    4. Provide an API, allows applications to query the coarse-grained or fine-grained state of the available networks provides APIs to Apply query coarse or fine network state

(http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html)

With these, we print out the broadcast when the network changes are received. The network status passed in is different from the current active network state. The code is as follows:

1 @Override2      Public voidOnReceive (Context context, Intent Intent) {3String action =intent.getaction ();4LOG.I ("Myreceiver", action);5         if(ConnectivityManager.CONNECTIVITY_ACTION.equals (ACTION)) {6Bundle B =Intent.getextras ();7             if(b = =NULL){8LOG.I ("Myreceiver", "b = = null");9                 return ;Ten             } OneNetworkinfo NetInfo =(Networkinfo) b.get (connectivitymanager.extra_network_info); ANetworkinfo.state State =netinfo.getstate (); -             intNetinfotype =Netinfo.gettype (); -              theConnectivitymanager cm =(Connectivitymanager) Context.getsystemservice (context.connectivity_service); -             if(cm = =NULL){ -LOG.I ("Myreceiver", "Connectivitymanager = = NULL"); -                 return ; +             } -Networkinfo Activenetinfo =cm.getactivenetworkinfo (); +             if(state! =NULL){ ALOG.I ("Myreceiver", State.name ()); atLOG.I ("Myreceiver", "state:" + netinfo.gettypename () + ":" +Netinfo.gettype ()); -}Else{ -LOG.I ("Myreceiver", "state = = NULL"); -             } -              -              in             if(Activenetinfo! =NULL){ -                 intActivenettype =Activenetinfo.gettype (); toLOG.I ("Myreceiver", Activenetinfo.gettypename () + ":" +Activenetinfo.gettype ()); +}Else{          
LOG.I ("Myreceiver", "activenetinfo = = NULL"); + } $ $ } - -}

To switch between networks using your phone, print as follows:

WiFi---> 3G
03-17 17:56:45.526:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:45.526:i/myreceiver (13406): Disconnected
03-17 17:56:45.526:i/myreceiver (13406): state:mobile:0
03-17 17:56:45.531:i/myreceiver (13406): Activenetinfo = = NULL

03-17 17:56:45.991:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:45.991:i/myreceiver (13406): CONNECTED
03-17 17:56:45.991:i/myreceiver (13406): state:wifi:1
03-17 17:56:45.991:i/myreceiver (13406): Activenetinfo = = NULL

03-17 17:56:49.491:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:49.496:i/myreceiver (13406): Disconnected
03-17 17:56:49.496:i/myreceiver (13406): state:wifi:1
03-17 17:56:49.496:i/myreceiver (13406): mobile:0

03-17 17:56:49.966:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 17:56:49.966:i/myreceiver (13406): CONNECTED
03-17 17:56:49.966:i/myreceiver (13406): state:mobile:0
03-17 17:56:49.966:i/myreceiver (13406): mobile:0

3g-->wifi

03-17 18:00:35.286:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:00:35.286:i/myreceiver (13406): CONNECTED
03-17 18:00:35.286:i/myreceiver (13406): state:wifi:1
03-17 18:00:35.291:i/myreceiver (13406): wifi:1

03-17 18:00:40.641:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:00:40.641:i/myreceiver (13406): Disconnected
03-17 18:00:40.641:i/myreceiver (13406): state:mobile:0
03-17 18:00:40.641:i/myreceiver (13406): wifi:1

03-17 18:00:41.506:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:00:41.506:i/myreceiver (13406): CONNECTED
03-17 18:00:41.506:i/myreceiver (13406): state:wifi:1
03-17 18:00:41.506:i/myreceiver (13406): wifi:1


WiFi--3g--wifi
03-17 18:02:54.861:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:54.861:i/myreceiver (13406): Disconnected
03-17 18:02:54.861:i/myreceiver (13406): state:wifi:1
03-17 18:02:54.861:i/myreceiver (13406): wifi:1

03-17 18:02:55.331:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:55.331:i/myreceiver (13406): CONNECTED
03-17 18:02:55.331:i/myreceiver (13406): state:mobile:0
03-17 18:02:55.331:i/myreceiver (13406): wifi:1

03-17 18:02:56.771:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:56.771:i/myreceiver (13406): CONNECTED
03-17 18:02:56.771:i/myreceiver (13406): state:wifi:1
03-17 18:02:56.771:i/myreceiver (13406): wifi:1

03-17 18:02:57.171:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:57.171:i/myreceiver (13406): Disconnected
03-17 18:02:57.171:i/myreceiver (13406): state:mobile:0
03-17 18:02:57.171:i/myreceiver (13406): wifi:1

03-17 18:02:57.771:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:02:57.771:i/myreceiver (13406): CONNECTED
03-17 18:02:57.771:i/myreceiver (13406): state:wifi:1
03-17 18:02:57.771:i/myreceiver (13406): wifi:1


Turn off WiFi
03-17 18:08:06.381:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:08:06.381:i/myreceiver (13406): Disconnected
03-17 18:08:06.381:i/myreceiver (13406): state:wifi:1
03-17 18:08:06.386:i/myreceiver (13406): Activenetinfo = = NULL

Off 3G

03-17 18:09:23.366:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:09:23.366:i/myreceiver (13406): Disconnected
03-17 18:09:23.371:i/myreceiver (13406): state:mobile:0
03-17 18:09:23.371:i/myreceiver (13406): Activenetinfo = = NULL


Open wifi

03-17 18:09:52.096:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:09:52.096:i/myreceiver (13406): CONNECTED
03-17 18:09:52.096:i/myreceiver (13406): state:wifi:1
03-17 18:09:52.096:i/myreceiver (13406): wifi:1

Open 3G

03-17 18:10:42.976:i/myreceiver (13406): Android.net.conn.CONNECTIVITY_CHANGE
03-17 18:10:42.976:i/myreceiver (13406): CONNECTED
03-17 18:10:42.976:i/myreceiver (13406): state:mobile:0
03-17 18:10:42.976:i/myreceiver (13406): mobile:0

According to these states, we can summarize some rules when we switch the network:

1. Activenetinfo = = NULL when the network is in a disconnected state before switching

2. When the network state is different, generally in the middle state

3. Network switching is likely to send duplicate broadcasts (is that so?) Ask the expert to answer)

Add some judgment and then test the code as follows:

@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); LOG.I ("Myreceiver", action); if(ConnectivityManager.CONNECTIVITY_ACTION.equals (ACTION)) {Bundle B=Intent.getextras (); if(b = =NULL) {log.i ("Myreceiver", "b = = null"); return ; } networkinfo NetInfo=(Networkinfo) b.get (connectivitymanager.extra_network_info); Networkinfo.state State=netinfo.getstate (); intNetinfotype =Netinfo.gettype (); if(Myapplication.cachestate = =NULL) {myapplication.cachestate=State ; Myapplication.nettype= Netinfotype;//mobile network (switch between 2g/3g/4g) or WiFi}Else if(Myapplication.cachestate = = state && Myapplication.nettype = =Netinfo.gettype ()) {LOG.I ("Myreceiver", "state:" + state.name () + "--" + netinfo.gettypename () + ":" +Netinfo.gettype ()); LOG.I ("Myreceiver", "Same status broadcast"); return ; } Connectivitymanager cm=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); if(cm = =NULL) {log.i ("Myreceiver", "Connectivitymanager = = NULL"); return ; } networkinfo Activenetinfo=Cm.getactivenetworkinfo (); if(Activenetinfo! =NULL){                intActivenettype =Activenetinfo.gettype (); LOG.I ("Myreceiver", Activenetinfo.gettypename () + ":" +Activenetinfo.gettype ()); if(Activenettype! = netinfotype) {//type different think is intermediate state not processedLOG.I ("Myreceiver", "type different judgment in the middle state: not processed" ); }Else{myapplication.cachestate=State ; Myapplication.nettype=Netinfotype; LOG.I ("Myreceiver", "Current operation State:" + state.name () + "--" + netinfo.gettypename () + ":" +Netinfo.gettype ()); }            }Else{myapplication.cachestate=State ; Myapplication.nettype=Netinfotype; LOG.I ("Myreceiver", "activenetinfo = = NULL"); LOG.I ("Myreceiver", "Current operation State:" + state.name () + "--" + netinfo.gettypename () + ":" +Netinfo.gettype ()); }                    }    }}

Tested, has met my requirements. It just seems a little stupid. There should be a more concise and accurate method, I hope to have friends who know to provide ideas.

Android network switch sends multiple broadcast issues

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.