Hello android developers
I declared a broadcast receiver registered android.media.RINGER_MODE_CHANGED
. That is working fine, it get called every ringer mode changed.
The real problem is I need to know the ringer mode transition. I.e. I need to perform some logic when the ringer mode is changed from normal to silent and vice versa. If ringer mode is changed from silent to vibrate, do nothing.
All the research I did so far pointing to a fact that, at the moment you receive RINGER_MDOE_CHANGED, as it said, it is changed. You have no idea what the ringer mode is before that change.
From the API document, http://developer.android.com/reference/android/media/AudioManager.html#RINGER_MODE_CHANGED_ACTION stated that the intent contains EXTRA_RINGER_MODE in its extra. However, that doesn't help. That is the current setting, I can get it from AudioManager.getRingerMode()
.
Do you know any way I can query the last ringer mode? Hope you guys can help. You are my last hope. Thank you.
There is no way to get the previous ringer mode from the API, but you can achieve this by using the following pattern.
RingerModeMonitorService
) that holds a ringer mode, which is simply an int
type variable. Persist the int value before stopSelf()
using persistInt()
.onReceive()
, issue an intent to start RingerModeMonitorService
. Set the intent flag as the ringer mode.RingerModeMonitorService
. This is to initialize the ringer mode stored in RingerModeMonitorService
.Afterward, when your broadcast receiver receives ringer mode change and you want to know the previous mode, you can bind to RingerModeMonitorService and send it a Message
. RingerModeMonitorService read the persisted ringer mode and return using Message and Handler.
It sounds complicate but the idea is simple. On every ringer mode change, I jot down (Persist) the ringer mode value for next time reference. However, please make sure you read the value before you jot it down or it will be overwritten, then every time the value you read is the current ringer mode not the previous one.
http://developer.android.com/guide/components/bound-services.html#Messenger have more details on service communication