Android 实时监听网络(数据orWifi)

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

猜你喜欢

转载自blog.csdn.net/qq_32368129/article/details/79583646