App노자
[Android] TelephonyManager (단말기 정보) 본문
1. TelephonyManager란?
TelephonyManager는 단말의 통신 서비스 정보에 대한 액세스를 제공한다
Android는 기기의 통신 상태, SIM 일련번호, 네트워크 정보, 전화번호 등과 같은 휴대전화 관련 정보를
TelephonyManager 클래스를 사용하여 얻을 수 있다
일부 전화 통신 정보에 대한 액세스는 권한으로 보호되고 있으며 매니페스트 파일에 권한이 선언되어 있지 않으면
애플리케이션이 보호된 정보에 액세스 할 수 없으며 일부 정보는 기기나 네트워크의 상태에 따라 사용 불가능한 경우도 있다
https://developer.android.com/reference/android/telephony/TelephonyManager
TelephonyManager | Android Developers
developer.android.com
2. AndroidManifest.xml
<uses-permission android:name="android.permission.modify_phone_state" />
TelephonyManager을 사용하기 위해서는 Manifest file에 위의 내용을 작성한다
3. Kotlin 파일
class TelephonyInfo(private val context: Context) {
private val telephonyManager: TelephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
fun getNetworkOperatorName(): String {
return telephonyManager.networkOperatorName
}
fun getNetworkType(): String {
val networkType = telephonyManager.networkType
return when (networkType) {
TelephonyManager.NETWORK_TYPE_UNKNOWN -> "Unknown"
TelephonyManager.NETWORK_TYPE_GPRS -> "GPRS"
TelephonyManager.NETWORK_TYPE_EDGE -> "EDGE"
TelephonyManager.NETWORK_TYPE_UMTS -> "UMTS"
TelephonyManager.NETWORK_TYPE_HSDPA -> "HSDPA"
TelephonyManager.NETWORK_TYPE_HSUPA -> "HSUPA"
TelephonyManager.NETWORK_TYPE_HSPA -> "HSPA"
TelephonyManager.NETWORK_TYPE_LTE -> "LTE"
else -> "Other"
}
}
fun getPhoneNumber(): String {
return telephonyManager.line1Number ?: "Unknown"
}
}
fun USimCheck(context: Context) {
val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
Log.d("tag", "getCallState : " + telephonyManager.callState)
Log.d("tag", "getDataActivity : " + telephonyManager.dataActivity)
Log.d("tag", "getDataState : " + telephonyManager.dataState)
Log.d("tag", "getDeviceId : " + telephonyManager.deviceId)
Log.d("tag", "getDeviceSoftwareVersion : " + telephonyManager.deviceSoftwareVersion)
Log.d("tag", "getLine1Number : " + telephonyManager.line1Number)
Log.d("tag", "getNetworkCountryIso : " + telephonyManager.networkCountryIso)
Log.d("tag", "getNetworkOperator : " + telephonyManager.networkOperator)
Log.d("tag", "getNetworkOperatorName : " + telephonyManager.networkOperatorName)
Log.d("tag", "getNetworkType : " + telephonyManager.networkType)
Log.d("tag", "getPhoneType : " + telephonyManager.phoneType)
Log.d("tag", "getSimCountryIso : " + telephonyManager.simCountryIso)
Log.d("tag", "getSubscriberId : " + telephonyManager.subscriberId)
Log.d("tag", "getVoiceMailAlphaTag : " + telephonyManager.voiceMailAlphaTag)
Log.d("tag", "getVoiceMailNumber : " + telephonyManager.voiceMailNumber)
Log.d("tag", "isNetworkRoaming : " + telephonyManager.isNetworkRoaming)
Log.d("tag", "hasIccCard : " + telephonyManager.hasIccCard())
Log.d("tag", "hashCode : " + telephonyManager.hashCode())
}
val telephonyInfo = TelephonyInfo(context)
val operatorName = telephonyInfo.getNetworkOperatorName()
val networkType = telephonyInfo.getNetworkType()
val phoneNumber = telephonyInfo.getPhoneNumber()
TelephonyInfo 클래스의 객체를 생성한 후에 해당 메서드를 호출하여 정보를 얻을 수 있다
'Android > AndroidStudio' 카테고리의 다른 글
[Android] 기존의 레이아웃을 데이터 바인딩 레이아웃으로 자동으로 변환 (0) | 2023.10.30 |
---|---|
[Android] 안드로이드 SHA-1 인증 정보 (0) | 2023.10.08 |
[Android] Drawerlayout (드로어 레이아웃) (0) | 2023.08.12 |
[Android] android.os.Build (단말기 정보) (0) | 2023.07.27 |
[Android] Ripple Effect (0) | 2023.07.16 |