网络状态实时监测

想做一个断网时显示页面。从网上找到一个挺好的帖子,然后就试着写了一下真的还不错。

大体思路:网络变化时系统会发出广播,然后监听这个广播,对activity进行通知。

在清单文件中添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

写一个工具类来监听网络变化:  

  1. public class NetUtil {  
  2.     /** 
  3.      * 没有连接网络 
  4.      */  
  5.     private static final int NETWORK_NONE = -1;  
  6.     /** 
  7.      * 移动网络 
  8.      */  
  9.     private static final int NETWORK_MOBILE = 0;  
  10.     /** 
  11.      * 无线网络 
  12.      */  
  13.     private static final int NETWORK_WIFI = 1;  
  14.   
  15.     public static int getNetWorkState(Context context) {  
  16.         // 得到连接管理器对象  
  17.         ConnectivityManager connectivityManager = (ConnectivityManager) context  
  18.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
  19.   
  20.         NetworkInfo activeNetworkInfo = connectivityManager  
  21.                 .getActiveNetworkInfo();  
  22.         if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {  
  23.   
  24.             if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {  
  25.                 return NETWORK_WIFI;  
  26.             } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {  
  27.                 return NETWORK_MOBILE;  
  28.             }  
  29.         } else {  
  30.             return NETWORK_NONE;  
  31.         }  
  32.         return NETWORK_NONE;  
  33.     }  
  34. }  
写一个类继承 BroadcastReceiver 自定义接口来做通知
  1. public class NetBroadcastReceiver extends BroadcastReceiver {  
  2.   
  3.     public NetEvevt evevt = BaseActivity.evevt;  
  4.   
  5.     @Override  
  6.     public void onReceive(Context context, Intent intent) {  
  7.         // TODO Auto-generated method stub  
  8.         // 如果相等的话就说明网络状态发生了变化  
  9.         if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {  
  10.             int netWorkState = NetUtil.getNetWorkState(context);  
  11.             // 接口回调传过去状态的类型  
  12.             evevt.onNetChange(netWorkState);  
  13.         }  
  14.     }  
  15.   
  16.     // 自定义接口  
  17.     public interface NetEvevt {  
  18.         public void onNetChange(int netMobile);  
  19.     }  
  20. }  
最后在 BaseActivity 中来实现自定义接顺便一提自己写一个继承与Activity的类是一个好的习惯,你可以在这个类里面写一些项目中多次调用的方法节省时间

  1. abstract public class BaseActivity extends FragmentActivity implements NetEvevt {  
  2.    
  3.     public static NetEvevt evevt;  
  4.     /** 
  5.      * 网络类型 
  6.      */  
  7.     private int netMobile;  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle arg0) {  
  11.         // TODO Auto-generated method stub  
  12.         super.onCreate(arg0);  
  13.         evevt = this;  
  14.         inspectNet();  
  15.     }  
  16.   
  17.   
  18.     /** 
  19.      * 初始化时判断有没有网络 
  20.      */  
  21.   
  22.     public boolean inspectNet() {  
  23.         this.netMobile = NetUtil.getNetWorkState(BaseActivity.this);  
  24.         return isNetConnect();  
  25.     }  
  26.   
  27.     /** 
  28.      * 网络变化之后的类型 
  29.      */  
  30.     @Override  
  31.     public void onNetChange(int netMobile) {  
  32.         // TODO Auto-generated method stub  
  33.         this.netMobile = netMobile;  
  34.         isNetConnect();  
  35.   
  36.     }  
  37.   
  38.     /** 
  39.      * 判断有无网络 。 
  40.      *  
  41.      * @return true 有网, false 没有网络. 
  42.      */  
  43.     public boolean isNetConnect() {  
  44.         if (netMobile == 1) {  
  45.             return true;  
  46.         } else if (netMobile == 0) {  
  47.             return true;  
  48.         } else if (netMobile == -1) {  
  49.             return false;  
  50.   
  51.         }  
  52.         return false;  
  53.     }  
  54. }  
最后别忘了,在清单文件中注册
  1. <receiver android:name="cn.broadcastreceiver.NetBroadcastReceiver" >  
  2.             <intent-filter>  
  3.                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />  
  4.             </intent-filter>  
  5.         </receiver>  
然后在activity中就可以实时监听到手机网络变化了
  1. public class MainActivity extends BaseActivity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         
  7.         setContentView(R.layout.activity_main);  
  8.         
  9.         }  
  10.   
  11. @Override  
  12.     public void onNetChange(int netMobile) {  
  13.         // TODO Auto-generated method stub  
  14.         //在这个判断,根据需要做处理  
  15.     }  
  16.   
  17.      
  18.   
  19.      
  20. }  
原文转载至http://blog.csdn.net/mxiaoyem/article/details/50708052

猜你喜欢

转载自blog.csdn.net/jia_you1/article/details/77344525