package com.xw.common.utils
|
|
import android.content.BroadcastReceiver
|
import android.content.Context
|
import android.content.Intent
|
import android.content.IntentFilter
|
import android.location.LocationManager
|
|
class GpsStatusReceiver(private val onGpsStatusChanged: (Boolean) -> Unit) : BroadcastReceiver() {
|
|
override fun onReceive(context: Context, intent: Intent) {
|
if (intent.action == LocationManager.PROVIDERS_CHANGED_ACTION) {
|
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
val isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|
onGpsStatusChanged(isGpsEnabled)
|
}
|
}
|
|
companion object {
|
fun register(context: Context, receiver: GpsStatusReceiver) {
|
val filter = IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)
|
context.registerReceiver(receiver, filter)
|
}
|
|
fun unregister(context: Context, receiver: GpsStatusReceiver) {
|
context.unregisterReceiver(receiver)
|
}
|
}
|
}
|