1

I have added dispatchKeyEvent and onKeyDown to detect volume change events inside activity which is working fine with devices having physical volume buttons. In the case of Huawei mate 30 pro above events are not firing as it doesn't have physical volume buttons but a gesture to control volume. Any solution ??

 override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    d(TAG, "signal onKeyDown $keyCode")
    when (keyCode) {
        KeyEvent.KEYCODE_HOME -> {
            d(TAG, "signal onKeyDown KEYCODE_HOME")
            if (alertDialog != null && alertDialog?.isShowing == true) {
                releaseDataAndFinishActivity()
            }
           
            return false
        }
        KeyEvent.KEYCODE_BACK -> {
            onBackPressed()
            return false
        }
        KeyEvent.KEYCODE_VOLUME_DOWN -> {
            updateVolumeOnKeyPress(false)
            return true
        }
        else -> return true
    }
}

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        updateVolumeOnKeyPress(true)
        return true
    }
    return false
}

override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
    d(TAG, "signal dispatchKeyEvent $event")
    return super.dispatchKeyEvent(event)
}

1 Answers1

0

About How to Detect Volume Change events in Activity on the Mate30 Pro, You can achieve this by listening to the android.media.VOLUME_CHANGED_ACTION broadcast.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • I cannot use this action in prod because it's not working for all devices. Secondly, it is not part of SDK and it might be removed in the future. **See** [link](https://stackoverflow.com/a/4764881/15791414) – Muhammad Ali Aug 16 '21 at 07:49
  • According to the team, Currently Android does not define such hardware. This is provided by Huawei and does not have standard interfaces. If you don't want to use it, there may hava no other way. – zhangxaochen Aug 16 '21 at 08:23