安卓实时监听网络变化

思路:使用长驻型广播注册广播接收器,重写BroadcastReceiver的onReceive方法对网络状态的变化做相应处理

1、AndroidManifest里注册广播接收器
安卓实时监听网络变化_第1张图片

2、重写广播接收器继承自BroadcastReceiver


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;

public class NetworkConnectChangedReceiver extends BroadcastReceiver {
    private String getConnectionType(int type) {
        String connType = "";
        if (type == ConnectivityManager.TYPE_MOBILE) {
            connType = "3G网络数据";
        } else if (type == ConnectivityManager.TYPE_WIFI) {
            connType = "WIFI网络";
        }
        return connType;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 监听wifi的打开与关闭,与wifi的连接无关
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.e("xiaoqiang", "wifiState:" + wifiState);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    break;
            }
        }
        // 监听wifi的连接状态即是否连上了一个有效无线路由
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent
                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                // 获取联网状态的NetWorkInfo对象
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                //获取的State对象则代表着连接成功与否等状态
                NetworkInfo.State state = networkInfo.getState();
                //判断网络是否已经连接
                boolean isConnected = state == NetworkInfo.State.CONNECTED;
                Log.e("xiaoqiang", "isConnected:" + isConnected);
                if (isConnected) {
                } else {

                }
            }
        }
        // 监听网络连接,包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            //获取联网状态的NetworkInfo对象
            NetworkInfo info = intent
                    .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                //如果当前的网络连接成功并且网络连接可用
                if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                    if (info.getType() == ConnectivityManager.TYPE_WIFI
                            || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                        Log.i("xiaoqiang", getConnectionType(info.getType()) + "连上");
                        Toast.makeText(context, getConnectionType(info.getType()) + "连接成功", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Log.i("xiaoqiang", getConnectionType(info.getType()) + "断开");
                    Toast.makeText(context, getConnectionType(info.getType()) + "断开", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

这样便轻松实现了app对网络状态的实时监听
参考:http://blog.csdn.net/qq_20785431/article/details/51520459

你可能感兴趣的:(小知识点,网络,网络实时监听)