Android 实时监测(监听)网络连接状态变化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mr_jianrong/article/details/78991188
看了网上的很多,都不是自己想要的。所以自己就参考着写了一个。
实现这个的方法很多,我是想的这种,如果哪有不足,有bug的地方希望大家指出,共同进步。。。。
先简单说一下思路:网络变化时系统会发出广播。所以我们监听这个广播,利用接口回调通知activity做相应的操作就好了。。
步骤:
           1、写个判断网络的工具类.
           2、先写个类继承BroadcastReceiver。(不要忘记在清单文件中注册)
          (谢谢ITzxl的提醒)需要在清单文件中添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
           3、写个回调接口
           4、BaseActivity实现这个接口


  1. /** 
  2.  *  
  3.  * @author cj 判断网络工具类 
  4.  *  
  5.  */  
  6. public class NetUtil {  
  7.     /** 
  8.      * 没有连接网络 
  9.      */  
  10.     private static final int NETWORK_NONE = -1;  
  11.     /** 
  12.      * 移动网络 
  13.      */  
  14.     private static final int NETWORK_MOBILE = 0;  
  15.     /** 
  16.      * 无线网络 
  17.      */  
  18.     private static final int NETWORK_WIFI = 1;  
  19.   
  20.     public static int getNetWorkState(Context context) {  
  21.         // 得到连接管理器对象  
  22.         ConnectivityManager connectivityManager = (ConnectivityManager) context  
  23.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  24.   
  25.         NetworkInfo activeNetworkInfo = connectivityManager  
  26.                 .getActiveNetworkInfo();  
  27.         if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {  
  28.   
  29.             if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {  
  30.                 return NETWORK_WIFI;  
  31.             } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {  
  32.                 return NETWORK_MOBILE;  
  33.             }  
  34.         } else {  
  35.             return NETWORK_NONE;  
  36.         }  
  37.         return NETWORK_NONE;  
  38.     }  
  39. }  
  40.   
  41.   
  42.   
  43. /** 
  44.  * 自定义检查手机网络状态是否切换的广播接受器 
  45.  *  
  46.  * @author cj 
  47.  *  
  48.  */  
  49. public class NetBroadcastReceiver extends BroadcastReceiver {  
  50.   
  51.     public NetEvevt evevt = BaseActivity.evevt;  
  52.   
  53.     @Override  
  54.     public void onReceive(Context context, Intent intent) {  
  55.         // TODO Auto-generated method stub  
  56.         // 如果相等的话就说明网络状态发生了变化  
  57.         if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {  
  58.             int netWorkState = NetUtil.getNetWorkState(context);  
  59.             // 接口回调传过去状态的类型  
  60.             evevt.onNetChange(netWorkState);  
  61.         }  
  62.     }  
  63.   
  64.     // 自定义接口  
  65.     public interface NetEvevt {  
  66.         public void onNetChange(int netMobile);  
  67.     }  
  68. }  
  69.   
  70.   
  71. 记得在manifest中注册  
  72. <receiver android:name="cn.broadcastreceiver.NetBroadcastReceiver" >  
  73.             <intent-filter>  
  74.                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  
  75.             </intent-filter>  
  76.         </receiver>  
  77.   
  78.   
  79.   
  80. abstract public class BaseActivity extends FragmentActivity implements NetEvevt {  
  81.    
  82.     public static NetEvevt evevt;  
  83.     /** 
  84.      * 网络类型 
  85.      */  
  86.     private int netMobile;  
  87.   
  88.     @Override  
  89.     protected void onCreate(Bundle arg0) {  
  90.         // TODO Auto-generated method stub  
  91.         super.onCreate(arg0);  
  92.         evevt = this;  
  93.         inspectNet();  
  94.     }  
  95.   
  96.   
  97.     /** 
  98.      * 初始化时判断有没有网络 
  99.      */  
  100.   
  101.     public boolean inspectNet() {  
  102.         this.netMobile = NetUtil.getNetWorkState(BaseActivity.this);  
  103.         return isNetConnect();  
  104.   
  105.         // if (netMobile == 1) {  
  106.         // System.out.println("inspectNet:连接wifi");  
  107.         // } else if (netMobile == 0) {  
  108.         // System.out.println("inspectNet:连接移动数据");  
  109.         // } else if (netMobile == -1) {  
  110.         // System.out.println("inspectNet:当前没有网络");  
  111.         //  
  112.         // }  
  113.     }  
  114.   
  115.     /** 
  116.      * 网络变化之后的类型 
  117.      */  
  118.     @Override  
  119.     public void onNetChange(int netMobile) {  
  120.         // TODO Auto-generated method stub  
  121.         this.netMobile = netMobile;  
  122.         isNetConnect();  
  123.   
  124.     }  
  125.   
  126.     /** 
  127.      * 判断有无网络 。 
  128.      *  
  129.      * @return true 有网, false 没有网络. 
  130.      */  
  131.     public boolean isNetConnect() {  
  132.         if (netMobile == 1) {  
  133.             return true;  
  134.         } else if (netMobile == 0) {  
  135.             return true;  
  136.         } else if (netMobile == -1) {  
  137.             return false;  
  138.   
  139.         }  
  140.         return false;  
  141.     }  
  142.   
  143. }  
  144.   
  145.   
  146. public class MainActivity extends BaseActivity {  
  147.   
  148.     @Override  
  149.     protected void onCreate(Bundle savedInstanceState) {  
  150.         super.onCreate(savedInstanceState);  
  151.         
  152.         setContentView(R.layout.activity_main);  
  153.         
  154.         }  
  155.   
  156. @Override  
  157.     public void onNetChange(int netMobile) {  
  158.         // TODO Auto-generated method stub  
  159.         //在这个判断,根据需要做处理  
  160.     }  
  161.   
  162.      
  163.   
  164.      
  165. }  

在这需要说明一下,手机在开着wifi长时间不用,自动黑屏长时间,会关闭流量,所以在下拉刷新的时候,把监测状态的提升语给隐藏了!


注释写的已经很清楚了,就不在啰嗦了。。。
如有不足的地方请指出  谢谢!


猜你喜欢

转载自blog.csdn.net/mr_jianrong/article/details/78991188