当前位置:   article > 正文

Android监听网络状态_android 网络监听

android 网络监听

 写一个广播咯,监听网络情况

  1. public class NetReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String action = intent.getAction();
  5. if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
  6. boolean isConnected = NetUtils.isNetworkConnected(context);
  7. System.out.println("网络状态:" + isConnected);
  8. System.out.println("wifi状态:" + NetUtils.isWifiConnected(context));
  9. System.out.println("移动网络状态:" + NetUtils.isMobileConnected(context));
  10. System.out.println("网络连接类型:" + NetUtils.getConnectedType(context));
  11. if (isConnected) {
  12. EventBus.getDefault().postSticky(new NetEvent(true));
  13. if (onNetConnect != null) {
  14. onNetConnect.onNetConnect();
  15. }
  16. } else {
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号