Android之监测手机网络状态的广播

今天具体说下Android检测网络状态的广播,我们在做一些手机应用的时候如果网络发生改变可能会给用户造成一些损失,在中国2G,3G网络都没有普及的情况下,基本都是包流量的,所以在做一些视频应用软件的时候,如果用户在使用WIFI的时候如果无线网络中断,手机网络会自动换手机网络,从而给用户造成不必要的损失。

Android手机在对于一些系统广播,如短信的接收,电话的接收,电池电量过低,网络状态改变都会发一个广播,既然系统会发送一条广播,那么就需要一个接收器来处理这个广播。首先定义一个类继承NetworkChangeReceiver,重写onReceive()就行了。然后在OnReceive()这个方法进行相应广播的处理。

网络状态切换的广播类:

[java]  view plain  copy
  1. package com.test;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.net.ConnectivityManager;  
  7. import android.net.NetworkInfo.State;  
  8.   
  9. public class extends BroadcastReceiver {  
  10.   
  11.     @Override  
  12.     public void onReceive(Context context, Intent intent) {  
  13.         State wifiState = null;  
  14.         State mobileState = null;  
  15.         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  16.         wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
  17.         mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
  18.         if (wifiState != null && mobileState != null  
  19.                 && State.CONNECTED != wifiState  
  20.                 && State.CONNECTED == mobileState) {  
  21.             // 手机网络连接成功  
  22.         } else if (wifiState != null && mobileState != null  
  23.                 && State.CONNECTED != wifiState  
  24.                 && State.CONNECTED != mobileState) {  
  25.             // 手机没有任何的网络  
  26.         } else if (wifiState != null && State.CONNECTED == wifiState) {  
  27.             // 无线网络连接成功  
  28.         }  
  29.   
  30.     }  
  31.   
  32. }  

在上面这个接收类中OnReceive()方法,你可以在上面三个网络状态(只有手机网络,只有无线网络,没有任何网络)中进行相应的处理,然后在应用中注册广播,注册广播有2种方式,一种在androidmanifest.xml中注册,一种在java代码中注册。

第一种:

[html]  view plain  copy
  1. <receiver  
  2.     android:name="com.test.NetworkBroadcast"  
  3.     android:label="NetworkConnection" >  
  4.     <intent-filter>  
  5.         <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  
  6.     </intent-filter>  
  7. </receiver>  


第二种:

可以在Activity的onCreate()方法中注册广播,在Activity的onDestory()方法中卸载广播。

[java]  view plain  copy
  1. private BroadcastReceiver networkBroadcast=new BroadcastReceiver();  
[java]  view plain  copy
  1.         private void registerNetworkReceiver() {  
  2.     IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);  
  3.     this.registerReceiver(networkBroadcast, filter);  
  4. }  
  5.   
  6. private void unRegisterNetworkReceiver() {  
  7.         this.unregisterReceiver(networkBroadcast);  
  8. }  

注意:在接收类中的onReceive()方法中不要处理太多复杂逻辑问题,尤其耗时的操作。



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值