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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
This is one way to do it, you could just fix to a set volume instead of changing. My goal was to adjust system volume Service.
Also, avoid doing this only when needed.
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.htmlActually there is one way you can do in service by using Content Observer. It works like a broadcast receiver, listen to the event of changing content such as volume, contacts, call log...
Using the following code in your service
Don't forget to declare it in the Android Manifest.xml