首页 > 代码库 > 网络状态监听

网络状态监听

网络状态监听

定义一个广播接收器状态变化接口

  1. publicclassNetBroadcastReceiverextendsBroadcastReceiver{
  2. publicstaticArrayList<NetEventHandler> mListeners =newArrayList<NetEventHandler>();
  3. privatestaticString NET_CHANGE_ACTION ="android.net.conn.CONNECTIVITY_CHANGE";
  4. @Override
  5. publicvoid onReceive(Context context,Intent intent){
  6. Log.i("dsc","dsc");
  7. if(intent.getAction().equals(NET_CHANGE_ACTION)){
  8. MainActivity.mNetWorkState =NetUtil.getNetworkState(context);
  9. if(mListeners.size()>0)//通知接口完成加载
  10. for(NetEventHandler handler : mListeners){
  11. handler.onNetChange();
  12. }
  13. }
  14. }
  15. publicinterfaceNetEventHandler{
  16. publicvoid onNetChange();
  17. }
  18. }

网络状态工具类

  1. publicclassNetUtil{
  2. publicstaticfinalint NETWORN_NONE =0;
  3. publicstaticfinalint NETWORN_WIFI =1;
  4. publicstaticfinalint NETWORN_MOBILE =2;
  5. publicstaticint getNetworkState(Context context){
  6. ConnectivityManager connManager =(ConnectivityManager) context
  7. .getSystemService(Context.CONNECTIVITY_SERVICE);
  8. // Wifi
  9. State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
  10. .getState();
  11. if(state ==State.CONNECTED || state ==State.CONNECTING){
  12. return NETWORN_WIFI;
  13. }
  14. // 3G
  15. state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
  16. .getState();
  17. if(state ==State.CONNECTED || state ==State.CONNECTING){
  18. return NETWORN_MOBILE;
  19. }
  20. return NETWORN_NONE;
  21. }
  22. }

主要实现

  1. publicclassMainActivityextendsAppCompatActivityimplementsNetBroadcastReceiver.NetEventHandler{
  2. publicstaticint mNetWorkState;
  3. privateNetBroadcastReceiver netReceiver;//网络状态监测
  4. privateboolean netState =false;
  5. @Override
  6. protectedvoid onCreate(Bundle savedInstanceState){
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. //注册网络监听广播
  10. regist();
  11. }
  12. @Override
  13. publicvoid onDestroy(){
  14. super.onDestroy();
  15. Log.e("myLog","TaskListFragment执行销毁");
  16. unregisterReceiver(netReceiver);
  17. }
  18. /**
  19. * 注册广播
  20. */
  21. privatevoid regist(){
  22. netReceiver =newNetBroadcastReceiver();
  23. IntentFilter filter =newIntentFilter();
  24. filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
  25. registerReceiver(netReceiver, filter);
  26. NetBroadcastReceiver.mListeners.add(this);
  27. }
  28. @Override
  29. publicvoid onNetChange(){
  30. if(getApplicationContext()==null){
  31. return;
  32. }
  33. if(NetUtil.getNetworkState(this)==NetUtil.NETWORN_NONE){//没有网络
  34. netState =false;
  35. Toast.makeText(this,"你的网络已断开连接,请检查网络!",Toast.LENGTH_SHORT).show();
  36. }elseif(NetUtil.getNetworkState(this)==NetUtil.NETWORN_WIFI){//wifi网络
  37. netState =true;
  38. Toast.makeText(this,"你目前是wifi网络!",Toast.LENGTH_SHORT).show();
  39. }else{//手机网络
  40. netState =true;
  41. Toast.makeText(this,"你目前是手机网络!",Toast.LENGTH_SHORT).show();
  42. }
  43. }
  44. }

AndroidManifest注册

  1. <receiverandroid:name=".NetBroadcastReceiver"/>



来自为知笔记(Wiz)



网络状态监听