Android 有线和无线网监听

全类型(gprs、以太网、wifi)网络连接状态监听:

网络状态监听

public class NetWorkChangReceiver extends BroadcastReceiver {

    private NetWorkChangeListener netWorkChangeListener;

    public NetWorkChangReceiver(NetWorkChangeListener netWorkChangeListener) {
        this.netWorkChangeListener = netWorkChangeListener;
    }

    public static interface NetWorkChangeListener{
        void connect();
        void disConnect();
    }
    /**
     * 获取连接类型
     *
     * @param type
     * @return
     */
    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) {
        String action = intent.getAction();

        if ( ConnectivityManager.CONNECTIVITY_ACTION.equals(action)
                || "android.net.conn.CONNECTIVITY_CHANGE".equals(action)) {

            switch (isNetworkAvailable()) {
                case NET_ETHERNET:
                    System.out.println("-----------networktest---------有线");
                    netWorkChangeListener.connect();
                case NET_WIFI:
                    System.out.println("-----------networktest---------无线");
                    netWorkChangeListener.connect();
                    break;
                case NET_MOBILE:
                    System.out.println("-----------networktest---------GPRS");
                    netWorkChangeListener.connect();
                    break;
                case NET_NOCONNECT:
                    System.out.println("-----------networktest---------无网络");
                    netWorkChangeListener.disConnect();
                    break;
                default:
                    break;
            }
        }
    }
}

检测网络是否可用:

public class NetUtil {
    public static final int NET_ETHERNET = 1;
    public static final int NET_WIFI = 2;
    public static final int NET_MOBILE = 3;
    public static final int NET_NOCONNECT = 0;


    public static int isNetworkAvailable() {
        ConnectivityManager connectMgr = (ConnectivityManager) QfAdTvApp.getApp().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ethNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


        if (ethNetInfo != null && ethNetInfo.isConnected()) {
            return NET_ETHERNET;
        } else if (wifiNetInfo != null && wifiNetInfo.isConnected()) {
            return NET_WIFI;
        } else if (mobileNetInfo != null && mobileNetInfo.isConnected()) {
            return NET_MOBILE;
        } else {
            return NET_NOCONNECT;
        }
    }


    /**
     * 检测网络是否可用
     *
     * @return
     */
    public static boolean isNetworkConnected( ) {
        return isNetworkAvailable() != NET_NOCONNECT;
    }
}

你可能感兴趣的:(android基础总结)