I have seen a few answers to similar types of questions, mostly considering the ContentObserver as its solution. Somewhere on the similar lines as under:
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, new ContentObserver(){...} );
But the Content Observer registered for the CONTENT_URI observes for a lot of other changes as well. Although, we can detect if it was a volume change by checking the difference between the previous volume and current volume, which also seems costly where we require an operation to fetch current volume and a variable to store the previous volume which I am looking to avoid.
So, is there a way we can listen for the Audio Volume Change only?
I got a solution that works on or above API 16.
MediaRoutercan be used which reports for the Media Route being used along with the Volume Change events.where setupCallback() is as follows: