Search code examples
androidservicebroadcastreceiver

when ringer mode is changed to normal stop service


when ringer mode is changed to normal how to do i stop my applications service. Im new to android programming, confused and just need quick example. I have seen other info online but could not make any sense of it.


Solution

  • For this scenario you need to monitor the system event for ringer mode change. In android platform there is an application component called broadcast receiver

    1. You need to create a new class subclassing BroadcastReceiver and register it through xml declaration or program code.
    2. Assign android.media.RINGER_MODE_CHANGED action to its intent filter
    3. Your BroadcastReceiver onReceive() method will be called with an intent as a parameter when the ringer mode is changed.
    4. Check current ringer mode by looking into the received intent extra with EXTRA_RINGER_MODE. And compare the value with RINGER_MODE_NORMAL
    5. Issue an intent to context.stopService() to stop your running service if the condition in point 4 satisfied