안드로이드에서 모바일 네트워크 구분하기 (2G, 3G, 4G, 5G)
프로그래밍/Android (Java)2021. 6. 2. 07:50
반응형
example.java
package com.example.mymobilenetworktype; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.telephony.TelephonyManager; import android.widget.TextView; import static android.telephony.TelephonyManager.NETWORK_TYPE_1xRTT; import static android.telephony.TelephonyManager.NETWORK_TYPE_CDMA; import static android.telephony.TelephonyManager.NETWORK_TYPE_EDGE; import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_0; import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_A; import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_B; import static android.telephony.TelephonyManager.NETWORK_TYPE_GPRS; import static android.telephony.TelephonyManager.NETWORK_TYPE_HSDPA; import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPA; import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPAP; import static android.telephony.TelephonyManager.NETWORK_TYPE_IDEN; import static android.telephony.TelephonyManager.NETWORK_TYPE_LTE; import static android.telephony.TelephonyManager.NETWORK_TYPE_NR; import static android.telephony.TelephonyManager.NETWORK_TYPE_UMTS; public class MainActivity extends AppCompatActivity { private TextView textView; private TelephonyManager telephonyManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE}, PackageManager.PERMISSION_GRANTED); telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } switch (telephonyManager.getDataNetworkType()) { case NETWORK_TYPE_EDGE: case NETWORK_TYPE_GPRS: case NETWORK_TYPE_CDMA: case NETWORK_TYPE_IDEN: case NETWORK_TYPE_1xRTT: textView.setText("2G"); break; case NETWORK_TYPE_UMTS: case NETWORK_TYPE_HSDPA: case NETWORK_TYPE_HSPA: case NETWORK_TYPE_HSPAP: case NETWORK_TYPE_EVDO_0: case NETWORK_TYPE_EVDO_A: case NETWORK_TYPE_EVDO_B: textView.setText("3G"); break; case NETWORK_TYPE_LTE: textView.setText("4G"); break; case NETWORK_TYPE_NR: textView.setText("5G"); break; default: textView.setText("Unknown"); } } } |
반응형
'프로그래밍 > Android (Java)' 카테고리의 다른 글
안드로이드 와이파이 SSID 가져오기 (0) | 2021.06.02 |
---|---|
TextView 레이아웃 사이즈에 맞게 글자 크기 자동 조정하기 (0) | 2021.06.02 |
Ubuntu 18.04 에서 Android Studio 및 AVD 설치하기 (0) | 2021.05.21 |
Ubuntu 18.04 내에서 안드로이드 (Android) 에뮬레이터 구동하기 (0) | 2021.04.15 |
리눅스 CentOS, Ubuntu 에서 Android-x86 설치하기 (0) | 2021.03.25 |
댓글()