监听网络状态变化的Android代码

jopen 11年前

下面代码,监听网络状态变化,当网络连接开启或关闭时将调用回调函数。

使用方法,你需创建一个 实例,同时在你的onCreate()和onResume方法中调用 bind(this)
在onPause方法调用unbind()

然后你可以做两件事:
1.在任何时刻调用hasConnection()检查网络状态。
2.实现OnNetworkAvailableListener接口,网络状态变化时会得到通知。

你需要以下权限:

    android.permission.INTERNET        android.permission.ACCESS_NETWORK_STATE  
    public class ConnectivityReceiver extends BroadcastReceiver {                        public static interface OnNetworkAvailableListener {                public void onNetworkAvailable();                public void onNetworkUnavailable();            }                        private final ConnectivityManager connectivityManager;            private OnNetworkAvailableListener onNetworkAvailableListener;            private boolean connection = false;                        public ConnectivityReceiver(Context context) {                connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);                checkConnectionOnDemand();            }                        public void bind(Context context) {                IntentFilter filter = new IntentFilter();                filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);                context.registerReceiver(this, filter);                checkConnectionOnDemand();            }                        public void unbind(Context context) {                context.unregisterReceiver(this);            }                    private void checkConnectionOnDemand() {                final NetworkInfo info = connectivityManager.getActiveNetworkInfo();                if (info == null || info.getState() != State.CONNECTED) {                    if (connection == true) {                        connection = false;                        if (onNetworkAvailableListener != null) onNetworkAvailableListener.onNetworkUnavailable();                    }                }                else {                    if (connection == false) {                        connection = true;                        if (onNetworkAvailableListener != null) onNetworkAvailableListener.onNetworkAvailable();                    }                }            }                        @Override            public void onReceive(Context context, Intent intent) {                if (connection == true && intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {                    connection = false;                    if (onNetworkAvailableListener != null) {                        onNetworkAvailableListener.onNetworkUnavailable();                    }                }                else if (connection == false && !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {                    connection = true;                    if (onNetworkAvailableListener != null) {                        onNetworkAvailableListener.onNetworkAvailable();                    }                }            }                        public boolean hasConnection() {                return connection;            }                        public void setOnNetworkAvailableListener(OnNetworkAvailableListener listener) {                this.onNetworkAvailableListener = listener;            }                }