首页 > 系统 > Android > 正文

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

2019-10-22 23:17:07
字体:
来源:转载
供稿:网友

看了网上的很多,都不是自己想要的。所以自己就参考着写了一个。

实现这个的方法很多,我是想的这种,如果哪有不足,有bug的地方希望大家指出,共同进步。。。。

先简单说一下思路:网络变化时系统会发出广播。所以我们监听这个广播,利用接口回调通知activity做相应的操作就好了。。

步骤:

           1、写个判断网络的工具类.

           2、先写个类继承BroadcastReceiver。(不要忘记在清单文件中注册)
          (谢谢ITzxl的提醒)需要在清单文件中添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

           3、写个回调接口

           4、BaseActivity实现这个接口

上代码:

/**  *  * @author cj 判断网络工具类  *  */ public class NetUtil {   /**    * 没有连接网络    */   private static final int NETWORK_NONE = -1;   /**    * 移动网络    */   private static final int NETWORK_MOBILE = 0;   /**    * 无线网络    */   private static final int NETWORK_WIFI = 1;   public static int getNetWorkState(Context context) {     // 得到连接管理器对象     ConnectivityManager connectivityManager = (ConnectivityManager) context         .getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo activeNetworkInfo = connectivityManager         .getActiveNetworkInfo();     if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {       if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {         return NETWORK_WIFI;       } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {         return NETWORK_MOBILE;       }     } else {       return NETWORK_NONE;     }     return NETWORK_NONE;   } } /**  * 自定义检查手机网络状态是否切换的广播接受器  *  * @author cj  *  */ public class NetBroadcastReceiver extends BroadcastReceiver {   public NetEvevt evevt = BaseActivity.evevt;   @Override   public void onReceive(Context context, Intent intent) {     // TODO Auto-generated method stub     // 如果相等的话就说明网络状态发生了变化     if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {       int netWorkState = NetUtil.getNetWorkState(context);       // 接口回调传过去状态的类型       evevt.onNetChange(netWorkState);     }   }   // 自定义接口   public interface NetEvevt {     public void onNetChange(int netMobile);   } } 

记得在manifest中注册 

<receiver android:name="cn.broadcastreceiver.NetBroadcastReceiver" >       <intent-filter>         <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />       </intent-filter>     </receiver> abstract public class BaseActivity extends FragmentActivity implements NetEvevt {   public static NetEvevt evevt;   /**    * 网络类型    */   private int netMobile;   @Override   protected void onCreate(Bundle arg0) {     // TODO Auto-generated method stub     super.onCreate(arg0);     evevt = this;     inspectNet();   }   /**    * 初始化时判断有没有网络    */   public boolean inspectNet() {     this.netMobile = NetUtil.getNetWorkState(BaseActivity.this);     return isNetConnect();     // if (netMobile == 1) {     // System.out.println("inspectNet:连接wifi");     // } else if (netMobile == 0) {     // System.out.println("inspectNet:连接移动数据");     // } else if (netMobile == -1) {     // System.out.println("inspectNet:当前没有网络");     //     // }   }   /**    * 网络变化之后的类型    */   @Override   public void onNetChange(int netMobile) {     // TODO Auto-generated method stub     this.netMobile = netMobile;     isNetConnect();   }   /**    * 判断有无网络 。    *    * @return true 有网, false 没有网络.    */   public boolean isNetConnect() {     if (netMobile == 1) {       return true;     } else if (netMobile == 0) {       return true;     } else if (netMobile == -1) {       return false;     }     return false;   } } public class MainActivity extends BaseActivity {   @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     } @Override   public void onNetChange(int netMobile) {     // TODO Auto-generated method stub     //在这个判断,根据需要做处理   } } 

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

android,监听网络状态

 


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表