본문 바로가기

Android

[Android] 현재 인터넷 연결 종류 String으로 가져오기

StackOverFlow의 도움을 많이 받았다.

기존 코드에 5G가 구분이 되지 않아서 isNRConnected를 추가해주었다.

퍼미션으로 ACCESS_NETWORK_STATEREAD_PHONE_STATE가 필요하다.

isNRConnected에서만 READ_PHONE_STATE가 필요하기 때문에 위험 권한인 READ_PHONE_STATE를 추가하기 싫다면 isNRConnected를 쓰지 않으면 된다. 단, 5G도 4G로 나올 확률이 크다.

// 5G 확인
static boolean isNRConnected(TelephonyManager telephonyManager) {
        try {
            Object obj = telephonyManager.getClass()
                    .getDeclaredMethod("getServiceState").invoke(telephonyManager);

            Method[] methods = Class.forName(obj.getClass().getName()).getDeclaredMethods();

            for (Method method : methods) {
                if (method.getName().equals("getNrStatus") || method.getName().equals("getNrState")) {
                    method.setAccessible(true);
                    return (Integer) method.invoke(obj) == 3;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
}
    public static String getNetworkConnectionType(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info == null || !info.isConnected())
            return "-"; // not connected
        if (info.getType() == ConnectivityManager.TYPE_WIFI)
            return "WIFI";
        if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            int networkType = info.getSubtype();
            switch (networkType) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:     // api< 8: replace by 11
                case TelephonyManager.NETWORK_TYPE_GSM:      // api<25: replace by 16
                    return "2G";
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:   // api< 9: replace by 12
                case TelephonyManager.NETWORK_TYPE_EHRPD:    // api<11: replace by 14
                case TelephonyManager.NETWORK_TYPE_HSPAP:    // api<13: replace by 15
                case TelephonyManager.NETWORK_TYPE_TD_SCDMA: // api<25: replace by 17
                    return "3G";
                case TelephonyManager.NETWORK_TYPE_LTE:      // api<11: replace by 13
                case TelephonyManager.NETWORK_TYPE_IWLAN:    // api<25: replace by 18
                case 19: // LTE_CA
                    if (isNRConnected((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)))
                        return "5G";
                    return "4G";
                case 20:       // api<29: replace by 20
                    return "5G";
                default:
                    return "?";
            }
        }
        return "?";
    }

 

코드 참고

 

이름 가져오기

https://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android#answer-25912464

 

5G 확인

https://stackoverflow.com/questions/55598359/how-to-detect-samsung-s10-5g-is-running-on-5g-network

 

How to detect Samsung S10 5G is running on 5G network?

Android Q added a new network type, NETWORK_TYPE_NR, for 5G, it is not available for Android Pie. Yet the just released Samsung S10 5G has fully supported 5G function, it can show a 5G icon on the ...

stackoverflow.com