Hello everyone,
I would like to create the classic Do Not Disturb App with the purpose of silencing notifications and ringtones at times specified by the user. The user could also choose a white list of contacts whose calls should not be silenced.
I tried to create a foreground service in which to insert a phone state listener. Below is a
test code. At the moment, when the service is active, the volume is raised for any incoming call. When the device stops ringing, the volume is lowered again.
I tried the code in a physical device with Android 8. There are no problems when debugging with ADB connected. Instead, when I disconnect the device and try to call it, after some attempts the volume is no longer raised (even if the notification of the foreground service is always active).
Manifest permissions:
Starting the service from the Activity (after having requested the previous permissions at runtime):
Service class:
Utility methods to mute/unmute sounds:
I also tried to add the request to put the app in the white list to ignore the battery optimizations (doze mode) but does not change anything.
I did a lot of trials and changes but I just can not figure out where the problem is. The strange thing is that when I try to call the device (after a few minutes that has entered standby), the volume remains lowered but after hanging up, if I open the App, the volume is raised and immediately lowered. As if the turnVolumeOn / turnVolumeOff calls remained on a waiting list.
I'm starting to think this app is above my ability. I really hope someone can help me understand. Thank you all for your attention