【android】解决方案--用BroadcastReceiver监听手机网络状态变化

实现一个功能,可以有很多种方法,但我们所追求的是最适合于自己项目的那一种方法。

就比如app要判断网络状态,如果在每次使用网络的时候去判断一次网络状态的话,有些耗费时间。例如,你要在打开网页之前先判断网络是否畅通以及在下载图片之前判断网络类型,那么务必造成等待时间的增加。因此,我们可以尝试只需在手机网络状态变更的时候,去记录一下当前的网络状态到我们的app里,在使用网络的地方就无需再去主动检查网络了。

废话不多说,本文主要是借助于android的广播机制BroadcastReceiver来接收系统在网络状态发生改变的时候发出的广播ConnectivityManager.CONNECTIVITY_ACTION即可。

首先、写一个BroadcastReceiver类:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import android.content.BroadcastReceiver;  
  2. import android.content.Context;  
  3. import android.content.Intent;  
  4. import android.net.ConnectivityManager;  
  5. import android.util.Log;  
  6. import android.widget.Toast;  
  7.   
  8. public class MyReceiver extends BroadcastReceiver {  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         String action = intent.getAction();  
  13.                 if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {  
  14.                      Toast.makeText(context, "myareceiver network changed", Toast.LENGTH_LONG).show();  
  15.                      Methods.refreshAPNType(context);  
  16.              new Thread(new Runnable() {  
  17.             @Override  
  18.             public void run() {  
  19.                                 //其中Methods为final类,参数netWorkType为当前应用中网络类型的标志  
  20.                                 //此处需要根据项目自己做修改  
  21.                 Methods.netWorkType = getAPNType(context);  
  22.             }  
  23.             }).start();  
  24.                 }  
  25.     }  
  26.   
  27.     /** 
  28.      * 获取当前的网络状态 :没有网络0:WIFI网络1:3G网络2:2G网络3 
  29.      *  
  30.      * @param context 
  31.      * @return 
  32.      */  
  33.     private static int getAPNType(Context context) {  
  34.         int netType = 0;  
  35.         ConnectivityManager connMgr = (ConnectivityManager) context  
  36.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  37.         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();  
  38.         if (networkInfo == null) {  
  39.             return netType;  
  40.         }  
  41.         int nType = networkInfo.getType();  
  42.         if (nType == ConnectivityManager.TYPE_WIFI) {  
  43.             netType = 1;// wifi  
  44.         } else if (nType == ConnectivityManager.TYPE_MOBILE) {  
  45.             int nSubType = networkInfo.getSubtype();  
  46.             TelephonyManager mTelephony = (TelephonyManager) context  
  47.                     .getSystemService(Context.TELEPHONY_SERVICE);  
  48.             if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS  
  49.                     && !mTelephony.isNetworkRoaming()) {  
  50.                 netType = 2;// 3G  
  51.             } else {  
  52.                 netType = 3;// 2G  
  53.             }  
  54.         }  
  55.         return netType;  
  56.     }  
  57. }  

其次,在app启动的activity中或AndroidManifest.xml中注册上述的广播接收者即可。

          方式一:在activity中注册广播接收者的代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. IntentFilter mFilter = new IntentFilter();  
  2.        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);  
  3.        MyReceiver mReceiver = new MyReceiver();  
  4.        registerReceiver(mReceiver, mFilter);  

         方式二:通过配置文件来进行静态注册:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <receiver  
  2. android:name="com.xxx.MyReceiver"  
  3. android:label="NetworkConnection" >  
  4.             <intent-filter>  
  5.                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  
  6.             </intent-filter>  
  7. </receiver>  

至此,一个完整的网络监听功能写完啦。



转自http://blog.csdn.net/wanggsx918/article/details/42918343

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值