android以太网,wifi,4g操作

首先需要如下权限:

工具类如下:

public class SignalUtils {

    /**
     * 判断是否有sim卡
     * author LH
     * created at 2018/5/28 15:01
     */
    public static boolean hasSimCard(Context context) {
        TelephonyManager telMgr = (TelephonyManager)
                context.getSystemService(Context.TELEPHONY_SERVICE);
        int simState = telMgr.getSimState();
        boolean result = true;
        switch (simState) {
            case TelephonyManager.SIM_STATE_ABSENT:
                result = false; // 没有SIM卡
                break;
            case TelephonyManager.SIM_STATE_UNKNOWN:
                result = false;
                break;
        }
        return result;
    }

    /**
     * 开关wifi
     * author LH
     * created at 2018/5/28 14:31
     */
    public static boolean toggleWiFi(Context context,boolean enabled) {
        try {
            WifiManager mWifiManager = (WifiManager)context.getApplicationContext().getSystemService(WIFI_SERVICE);
            return mWifiManager.setWifiEnabled(enabled);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断以太网是否可用
     * author LH
     * created at 2018/5/28 10:04
     */
    public static boolean isIntenetConnected(Context context) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mInternetNetWorkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        boolean hasInternet = mInternetNetWorkInfo != null && mInternetNetWorkInfo.isConnected() && mInternetNetWorkInfo.isAvailable();
        return hasInternet;
    }

    /**
     * 判断wifi是否可用
     * author LH
     * created at 2018/5/28 14:23
     */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mWifi != null) {
            return mWifi.isConnected();
        }
        return false;
    }

    /**
     * 判断移动网络是否可用
     * author LH
     * created at 2018/5/28 14:24
     */
    public static boolean isMobileConnected(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobile = connManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobile != null) {
            return mMobile.isConnected();
        }
        return false;
    }


}

 

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