Android获取当前网络类型

获取当前网络属于 无网络(返回0)、WF(返回1)、2G(返回2)、3G(返回3)、4G(返回4)、5G(返回5) 网络

使用方法:IntenetUtil.getNetworkState(getContext());

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * 获取当前网络属于 无网络、WF、2G、3G、4G、5G 网络
 * //没有网络连接
 * public static final int NETWORN_NONE = 0;
 * //wifi连接
 * public static final int NETWORN_WIFI = 1;
 * //手机网络数据连接类型
 * public static final int NETWORN_2G = 2;
 * public static final int NETWORN_3G = 3;
 * public static final int NETWORN_4G = 4;
 * public static final int NETWORN_MOBILE = 5;
 */
public class IntenetUtil {
    //没有网络连接
    public static final int NETWORN_NONE = 0;
    //wifi连接
    public static final int NETWORN_WIFI = 1;
    //手机网络数据连接类型
    public static final int NETWORN_2G = 2;
    public static final int NETWORN_3G = 3;
    public static final int NETWORN_4G = 4;
    public static final int NETWORN_5G = 5;
    public static final int NETWORN_MOBILE = 6;

    /**
     * 获取当前网络连接类型
     *
     * @param context
     * @return
     */
    public static int getNetworkState(Context context) {
        //获取系统的网络服务
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //如果当前没有网络
        if (null == connManager)
            return NETWORN_NONE;
        //获取当前网络类型,如果为空,返回无网络
        @SuppressLint("MissingPermission") NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
        if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
            return NETWORN_NONE;
        }
        // 判断是不是连接的是不是wifi
        @SuppressLint("MissingPermission") NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (null != wifiInfo) {
            NetworkInfo.State state = wifiInfo.getState();
            if (null != state)
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return NETWORN_WIFI;
                }
        }
        // 如果不是wifi,则判断当前连接的是运营商的哪种网络2g、3g、4g等
        @SuppressLint("MissingPermission") NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (null != networkInfo) {
            NetworkInfo.State state = networkInfo.getState();
            String strSubTypeName = networkInfo.getSubtypeName();
            if (null != state)
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    switch (activeNetInfo.getSubtype()) {
                        //如果是2g类型
                        case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
                        case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
                        case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                            return NETWORN_2G;
                        //如果是3g类型
                        case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                            return NETWORN_3G;
                        //如果是4g类型
                        case TelephonyManager.NETWORK_TYPE_LTE:
                            return NETWORN_4G;
                        case TelephonyManager.NETWORK_TYPE_NR: //对应的20 只有依赖为android 10.0才有此属性
                            return NETWORN_5G;
                        default:
                            //中国移动 联通 电信 三种3G制式
                            if (strSubTypeName.equalsIgnoreCase("TD-SCDMA") || strSubTypeName.equalsIgnoreCase("WCDMA") || strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                                return NETWORN_3G;
                            } else {
                                return NETWORN_MOBILE;
                            }
                    }
                }
        }
        return NETWORN_NONE;
    }
}

华为提供了自己的接口判断是否为5G网络

/**
     * get huawei network type
     * @return networkType
     */
    public static int getHwNetworkType(Context ctx) {
        int networkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
        if (VERSION.SDK_INT >= VERSION_CODES.O
                && ctx.checkSelfPermission(permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            try {
                TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
                ServiceState ss;
                int subId = SubscriptionManager.getDefaultDataSubscriptionId();
                if (subId < 0) {
                    ss = tm.getServiceState();
                } else {
                    Class<TelephonyManager> classTm = TelephonyManager.class;
                    Method method = classTm
                            .getDeclaredMethod("getServiceStateForSubscriber", new Class[]{int.class});
                    method.setAccessible(true);
                    Object objSs = method.invoke(tm, subId);
                    ss = (ServiceState) objSs;
                }

                Method hwGetNetworkMethod = ServiceState.class.getMethod("getHwNetworkType");
                hwGetNetworkMethod.setAccessible(true);
                Integer hwNetType = (Integer) hwGetNetworkMethod.invoke(ss);
                if (hwNetType != null) {
                    networkType = hwNetType;
                }
            } catch (Exception e) {
                QLog.e(tag, QLog.USR, "getHwNetworkType throw ex", e);
            }
        }
        return networkType;
    }
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
获取当前连接到的网络的城市和国家,你可以使用Geolocation API和Android网络连接管理器。以下是获取当前连接到的网络城市和国家的示例代码: ``` // 检查网络连接状态 ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { // 获取网络连接类型 int networkType = networkInfo.getType(); if (networkType == ConnectivityManager.TYPE_WIFI || networkType == ConnectivityManager.TYPE_MOBILE) { // 获取网络连接的IP地址 String ipAddress = getIPAddress(networkType); // 获取当前位置信息 Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocationName(ipAddress, 1); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String city = address.getLocality(); String country = address.getCountryName(); // 使用城市和国家信息 // ... } } } // 获取网络连接的IP地址 private String getIPAddress(int networkType) { if (networkType == ConnectivityManager.TYPE_WIFI) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff)); } else if (networkType == ConnectivityManager.TYPE_MOBILE) { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLinkLocalAddress()) { String ipAddress = inetAddress.getHostAddress().toString(); if (ipAddress.contains(".") && !ipAddress.contains(":")) { return ipAddress; } } } } } catch (Exception e) { e.printStackTrace(); } } return null; } ``` 注意:该方法可能不是100%准确,因为IP地址可能与实际位置不同,但在大多数情况下,它应该可以提供有用的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值