androidservicevolumedetect

any way to detect volume key presses or volume changes with android service?


Some Android apps generate a notification when the device's volume changes and some lock the volume. For the life of me, I cannot find out how that's done. Please, can someone help me by providing an example?


Solution

  • There is no Broadcast Action to detect volume changes, but you could maybe every second or two check what the volume is with getStreamVolume and if you need to lock it at a specific volume, every second or two use: setStreamVolume

    Check http://developer.android.com/reference/android/media/AudioManager.htm for more info.

    You could use the AlarmManager class or a handler to check the volume every second or so.

    If it's an activity, you can override onKeyDown to detect key presses. See http://developer.android.com/reference/android/view/View.html