I want to use my BroadcastReceiver as sender of data into my activity. For this reason I'm using LocalBroadcastManager. This manager is used to register and unregister my receiver. Problem is that Context in onReceive method is different than Context in onStart and onStop method.
I need to pass activity context into my BroadcastReceiver or instance of LocalBroadcastManager initialized inside Activity. Because my receiver is not receiving any data.
Maybe it is not fault of this manager context but I don't know why it doesnt work since I implemented this manager.
class GPSReceiver: BroadcastReceiver(){
    companion object{
        const val GPS_PAYLOAD = "gps_payload"
    }
    override fun onReceive(context: Context, intent: Intent) {
        try {
            val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
            val int = Intent(GPS_PAYLOAD)
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                int.putExtra(GPS_PAYLOAD, true)
            } else {
                int.putExtra(GPS_PAYLOAD, false)
            }
            LocalBroadcastManager.getInstance(context).sendBroadcast(int)
        } catch (ex: Exception) {
        }
    }
}
Registering receiver inside Activity:
private val gpsStatusReceiver = object: BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            App.log("isGpsEnabled: onReceive")
            val gpsStatus = intent?.extras?.getBoolean(GPS_PAYLOAD)
            if (gpsStatus != null) {
                if (gpsStatus){
                    App.log("isGpsEnabled: true")
                    hideGpsSnackbar()
                } else {
                    App.log("isGpsEnabled: false")
                    showGpsSnackbar()
                }
            } else {
                App.log("isGpsEnabled: null")
            }
        }
    }
override fun onStart() {
        super.onStart()
        LocalBroadcastManager.getInstance(this).apply {
            val filter = IntentFilter()
            filter.apply {
                addAction("android.location.PROVIDERS_CHANGED")
                addAction(GPS_PAYLOAD)
            }
            registerReceiver(gpsStatusReceiver, filter)
        }
    }
 
    