TOP

网络状态监听

网络状态监听

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

  1. public class NetBroadcastReceiver extends BroadcastReceiver {
  2. public static ArrayList<NetEventHandler> mListeners = new ArrayList<NetEventHandler>();
  3. private static String NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
  4. @Override
  5. public void 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. public interface NetEventHandler {
  16. public void onNetChange();
  17. }
  18. }

网络状态工具类

  1. public class NetUtil {
  2. public static final int NETWORN_NONE = 0;
  3. public static final int NETWORN_WIFI = 1;
  4. public static final int NETWORN_MOBILE = 2;
  5. public static int 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. public class MainActivity extends AppCompatActivity implements NetBroadcastReceiver.NetEventHandler{
  2. public static int mNetWorkState;
  3. private NetBroadcastReceiver netReceiver;//网络状态监测
  4. private boolean netState = false;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. //注册网络监听广播
  10. regist();
  11. }
  12. @Override
  13. public void onDestroy() {
  14. super.onDestroy();
  15. Log.e("myLog", "TaskListFragment执行销毁");
  16. unregisterReceiver(netReceiver);
  17. }
  18. /**
  19. * 注册广播
  20. */
  21. private void regist() {
  22. netReceiver = new NetBroadcastReceiver();
  23. IntentFilter filter = new IntentFilter();
  24. filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
  25. registerReceiver(netReceiver, filter);
  26. NetBroadcastReceiver.mListeners.add(this);
  27. }
  28. @Override
  29. public void 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. } else if (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. <receiver android:name=".NetBroadcastReceiver"/>




posted @ 2016-09-03 15:19  星空守候  阅读(464)  评论(0编辑  收藏  举报