1

I register my broadcast receiver once (debugged it), but onReceive() gets triggered 4 times.

private val locationBroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (LocationManager.PROVIDERS_CHANGED_ACTION == intent.action) {
            val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
            val locationEnabled = isLocationEnabled(locationManager)

            //..
        }
    }
}

Is there clean way of making sure that receiver gets triggered once on every user action? (GPS toogle on/off in this case)

MaaAn13
  • 264
  • 5
  • 24
  • 54
  • Does this help: https://stackoverflow.com/questions/31554676/broadcastreceiver-fires-multiple-times-providers-changed-action ? – Dan Baruch Nov 16 '20 at 13:16
  • @DanBaruch That feels very hacky solution for me. Was looking for something more cleaner, if there's any other approach. – MaaAn13 Nov 16 '20 at 13:19
  • 2
    Well, not sure if it will work, but maybe you can unregister your broadcast as soon as you get the intent, do all the logic you need and then re-register it? It's not fail proof but will definitely operate better. If I find any better approach I'll let you know – Dan Baruch Nov 16 '20 at 13:27
  • Facing same issues, in some devices its trigged 2 time, in other 4 times and in some only once. – A-run Apr 07 '21 at 09:58

0 Answers0