18034727885
2026-05-23 293183266e941692005262d76f0bcff05d6384d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)
        }
    }
}