Android 网络切换 发送多次广播问题
最近发现做项目监听网络切换广播,根据网络条件切换一些设置.测试发现每次3G-WIFI 或者WIFI到3G,网络切换的广播都会发出多次.比如3G-->WIFI
会发送三个广播 1.连接wifi 2.关闭手机网络 3.连接wifi 有没有方法判断这个过程呢?那就来看一个类
http://www.androidcommunitydocs.com/reference/android/net/ConnectivityManager.html (官方API 文档)
public class
ConnectivityManager extends Object
java.lang.Object
? android.net.ConnectivityManager
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).
The primary responsibilities of this class are to 该类主要职责:
- Monitor network connections (Wi-Fi, GPRS, UMTS, etc.) 监视网络连接
- Send broadcast intents when network connectivity changes 网络连接变化时发送广播
- Attempt to "fail over" to another network when connectivity to a network is lost 当当前网络不可用时尝试切换到其他网络
- Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks 提供API给应用查询粗略或精细的网络状态
(http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html)
有了这些接着我们打印一下,在接受到网络变化的广播时.传过来的网络状态与当前激活的网络状态有什么不同 代码如下 :
1 @Override 2 public void onReceive(Context context, Intent intent) { 3 String action = intent.getAction(); 4 Log.i("MyReceiver", action); 5 if(ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){ 6 Bundle b = intent.getExtras(); 7 if(b == null){ 8 Log.i("MyReceiver", "b == null "); 9 return ; 10 } 11 NetworkInfo netInfo = (NetworkInfo) b.get(ConnectivityManager.EXTRA_NETWORK_INFO); 12 NetworkInfo.State state = netInfo.getState(); 13 int netInfoType = netInfo.getType(); 14 15 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 16 if(cm == null){ 17 Log.i("MyReceiver", "ConnectivityManager == null"); 18 return ; 19 } 20 NetworkInfo activeNetInfo = cm.getActiveNetworkInfo(); 21 if(state != null){ 22 Log.i("MyReceiver", state.name()); 23 Log.i("MyReceiver", "state : " + netInfo.getTypeName() + " : " + netInfo.getType()); 24 }else{ 25 Log.i("MyReceiver", "state == null"); 26 } 27 28 29 if(activeNetInfo != null){ 30 int activeNetType = activeNetInfo.getType(); 31 Log.i("MyReceiver", activeNetInfo.getTypeName() + " : " + activeNetInfo.getType()); 32 }else{
Log.i("MyReceiver", "activeNetInfo == null "); 43 } 45 46 } 47 48 }
使用手机进行网络间切换,打印如下 :
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
关闭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
关闭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
打开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
打开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
根据这些状态,在网络切换的时候 我们可以总结出一些规律 :
1.网络在切换之前处于断开状态时 activeNetInfo == null
2.当网络状态不同时一般处于中间状态
3.网络切换有可能发送重复的广播 (是这样吗?求高手解答)
加入一些判断然后测试代码如下 :
@Override public void onReceive(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(); int netInfoType = netInfo.getType(); if(MyApplication.cacheState == null){ MyApplication.cacheState = state ; MyApplication.netType = netInfoType; // 移动网络 (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", " 相同 状态广播 "); 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){ int activeNetType = activeNetInfo.getType(); Log.i("MyReceiver", activeNetInfo.getTypeName() + " : " + activeNetInfo.getType()); if(activeNetType != netInfoType){ // 类型不同 认为是中间状态 不处理 Log.i("MyReceiver", "类型不同 判断处于中间状态 : 不处理 " ); }else{ MyApplication.cacheState = state ; MyApplication.netType = netInfoType; Log.i("MyReceiver", "当前操作 state : " + state.name() +" -- " + netInfo.getTypeName() + " : " + netInfo.getType()); } }else{ MyApplication.cacheState = state ; MyApplication.netType = netInfoType; Log.i("MyReceiver", "activeNetInfo == null "); Log.i("MyReceiver", "当前操作 state : " + state.name() +" -- " + netInfo.getTypeName() + " : " + netInfo.getType()); } } } }
测试了一下,已经满足了我的要求.只是看起来方法有点笨. 应该会有更加简洁准确的方法,希望有知道的朋友提供一下思路。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。