티스토리 뷰
스택오버플로우에는 자바코드 혹은 구식코드가 많아서 새로운 방법으로 알아냈다.
RSRP를 알애내는 부분이 깔끔하지 않은데 저 방법밖에 없나? 싶기도.
package com.studiox.signalstrength
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Build
import android.os.Bundle
import android.telephony.PhoneStateListener
import android.telephony.SignalStrength
import android.telephony.TelephonyManager
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
//id 설정
private lateinit var signalTxt:TextView
private lateinit var signalTxt2:TextView
private lateinit var signalTxt3:TextView
private lateinit var networkTypeTxt:TextView
override fun onStart() {
super.onStart()
}
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
signalTxt = findViewById<TextView>(R.id.signalTxt)
networkTypeTxt = findViewById<TextView>(R.id.networkTypeTxt)
signalTxt2 = findViewById<TextView>(R.id.signalTxt2)
signalTxt3 = findViewById<TextView>(R.id.signalTxt3)
var networkType = ""
//permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val res = checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
if (res != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.READ_PHONE_STATE), 123)
}
}
//텔레폰메니져
var telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
// 연결 확인
val conMan = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val mobile = conMan.getNetworkInfo(0)!!.state
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//모바일 연결 확인
}
//네트워크 종류
networkTypeTxt.text = networkType(telephonyManager.dataNetworkType)
// signal RSRP catch
val phoneListener = object : PhoneStateListener() {
override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
super.onSignalStrengthsChanged(signalStrength)
var mSignalStrength = signalStrength.cellSignalStrengths
for (str in mSignalStrength){
println("//" + str)
}
val signalStrength = 2 * signalStrength.gsmSignalStrength - 113
signalTxt.text = mSignalStrength.toString()
println(signalStrength)
val resultSignal = mSignalStrength.toString().split(" ")[2].replace("rsrp=", "").toInt()
signalTxt2.text = resultSignal.toString()
var signalGrade = ""
if(resultSignal >= -80){
signalGrade = ("Excellent")
}else if(resultSignal <= -80 && resultSignal >= -90){
signalGrade = ("Good")
}else if(resultSignal <= -90 && resultSignal >= -100){
signalGrade = ("poor")
}else if(resultSignal <= -100){
signalGrade = ("No signal")
}
signalTxt3.text = signalGrade
}
}
//1초마다 알아내기
val timer = Timer()
timer.schedule(object : TimerTask() {
@RequiresApi(Build.VERSION_CODES.P)
override fun run() {
println("================")
println(networkType)
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
}
}, 1000, 1000)
}
//네트워크 종류 알아내기
private fun networkType(type: Int): String? {
when (type) {
TelephonyManager.NETWORK_TYPE_1xRTT -> return "1xRTT"
TelephonyManager.NETWORK_TYPE_CDMA -> return "CDMA"
TelephonyManager.NETWORK_TYPE_EDGE -> return "EDGE"
TelephonyManager.NETWORK_TYPE_EHRPD -> return "eHRPD"
TelephonyManager.NETWORK_TYPE_EVDO_0 -> return "EVDO rev. 0"
TelephonyManager.NETWORK_TYPE_EVDO_A -> return "EVDO rev. A"
TelephonyManager.NETWORK_TYPE_EVDO_B -> return "EVDO rev. B"
TelephonyManager.NETWORK_TYPE_GPRS -> return "GPRS"
TelephonyManager.NETWORK_TYPE_HSDPA -> return "HSDPA"
TelephonyManager.NETWORK_TYPE_HSPA -> return "HSPA"
TelephonyManager.NETWORK_TYPE_HSPAP -> return "HSPA+"
TelephonyManager.NETWORK_TYPE_HSUPA -> return "HSUPA"
TelephonyManager.NETWORK_TYPE_IDEN -> return "iDen"
TelephonyManager.NETWORK_TYPE_LTE -> return "LTE"
TelephonyManager.NETWORK_TYPE_UMTS -> return "UMTS"
TelephonyManager.NETWORK_TYPE_UNKNOWN -> return "Unknown"
}
throw RuntimeException("New type of network")
}
}
'프로그래밍 > 안드로이드' 카테고리의 다른 글
웹플럭스 코틀린(Coroutine) 에서 post get 받는 방법 (0) | 2021.05.18 |
---|---|
LocalDateTime.now GMT 나올때 (0) | 2021.04.27 |
CrudRepository 와 JpaRepository 차이. (0) | 2021.03.11 |
kotlin android extensions 제거에 따른 버튼등의 id 부르는 방법 (0) | 2021.03.04 |
for문안 for의 break와 continue에 대한 정리 (0) | 2021.03.01 |