투케이2K

562. (Android/Java) ConnectivityManager getAllNetworks 사용해 내 휴대폰 인터넷 활성 상태 확인 (LTE , 테더링 핫스팟) 본문

Android

562. (Android/Java) ConnectivityManager getAllNetworks 사용해 내 휴대폰 인터넷 활성 상태 확인 (LTE , 테더링 핫스팟)

투케이2K 2023. 5. 21. 18:07

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java

 

[소스 코드]

 

        // -------------------------------------
        // [로직 처리 실시]
        // -------------------------------------
        try {

            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            if (connectivityManager != null) {

                Network[] allNetworks = connectivityManager.getAllNetworks();

                for (Network network: allNetworks) {
                    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);

                    S_Log._D_("네트워크 정보", new String[]{
                            "getState :: " + String.valueOf(networkInfo.getState()),
                            "getReason :: " + String.valueOf(networkInfo.getReason()),
                            "getTypeName :: " + String.valueOf(networkInfo.getTypeName()),
                            "getSubtypeName :: " + String.valueOf(networkInfo.getSubtypeName()),
                            "getExtraInfo :: " + String.valueOf(networkInfo.getExtraInfo())
                    });
                    
                    // [테더링 여부 체크]
                    if (networkInfo.getExtraInfo().toLowerCase().contains("tethering") == true){
                        // ------------------------------------------------
                        S_Log._W_("테더링 네트워크 확인", null);
                        // ------------------------------------------------
                    }
                }
            }

        }
        catch (Exception e){
            e.printStackTrace();
        }

반응형
Comments