안드로이드(Android)/코틀린(Kotlin)
[Android] 안드로이드 기기 위치정보 켜져있는지 여부확인하는 방법
리안94
2021. 1. 12. 15:26
위치정보를 받기 위해서는 위치정보 퍼미션도 중요하지만, 기기가 위치정보를 on/off 했는지 확인하는 것도 중요하다.
아래의 방법을 사용한다면 위치정보 on/off여부를 알 수있다.
fun isEnableLocationSystem(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager
locationManager?.isLocationEnabled!!
}else{
val mode = Settings.Secure.getInt(context.contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF)
mode != Settings.Secure.LOCATION_MODE_OFF
}
}
반환 값이 트루가 온다면 위치정보는 on상태이며, false가 오게 된다면 위치정보는 off상태이다.
더 사용하기 편하게 확장 함수로 표현하게 되면
fun Context.isEnableLocationSystem(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as? LocationManager
locationManager?.isLocationEnabled!!
}else{
val mode = Settings.Secure.getInt(contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF)
mode != Settings.Secure.LOCATION_MODE_OFF
}
}
이렇게 된다.