Intercept volume button press?

Cothek

Member
Licensed User
Longtime User
I currently have an alarm that plays at a given time and would like to be able to cancel the alarm by pressing either of the volume keys. This would allow the user to silence the alarm if they are in a meeting for example.

I have tried this method to no avail. What I did was create a new service and add the manifest lines from the link as well as the starting intent but pressing the volume buttons never started the service.

Any suggestions?
 

melamoud

Active Member
Licensed User
Longtime User
Try this , it worked for me
in yoiur manifest add the following: (my process name was QVMService,change it to yours)
B4X:
AddReceiverText(QVMService, <intent-filter>
    <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>)
   
AddReceiverText(QVMService, <intent-filter>
    <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>)

example of code I was using to find the previous/new volume and type you do not need it, just check if the action was volume change, it will work even if the volume was 0 and you pressed volume down, it will have an event of volume level 0 change to volume level 0, so you are safe

I never saw the event from the media buttons, just the volume change one.

B4X:
....

Dim prevVol As Int
Dim curVol As Int
Dim volType As Int

If (StartingIntent.HasExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE") AND StartingIntent.HasExtra("android.media.EXTRA_VOLUME_STREAM_VALUE") AND StartingIntent.Action = "android.media.VOLUME_CHANGED_ACTION") Then
      prevVol = StartingIntent.GetExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE")
      curVol = StartingIntent.GetExtra("android.media.EXTRA_VOLUME_STREAM_VALUE")
      volType = StartingIntent.GetExtra("android.media.EXTRA_VOLUME_STREAM_TYPE")
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you don't want to run anything via intent, but just know when the keys are pressed so the app can stop the alarm you just need to create the Activity_KeyPress sub like this:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   
   If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then 
      Log("Up")
                TurnOffAlarm
      Return True
   End If
   If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN Then 
      Log("Down")
                TurnOffAlarm
      Return True
   End If
End Sub

Remember to return true so that the volume is not changed at the same time.
 
Upvote 0

KY Leng

Member
Licensed User
Longtime User
Try this , it worked for me
in yoiur manifest add the following: (my process name was QVMService,change it to yours)
B4X:
AddReceiverText(QVMService, <intent-filter>
    <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>)
  
AddReceiverText(QVMService, <intent-filter>
    <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>)

example of code I was using to find the previous/new volume and type you do not need it, just check if the action was volume change, it will work even if the volume was 0 and you pressed volume down, it will have an event of volume level 0 change to volume level 0, so you are safe

I never saw the event from the media buttons, just the volume change one.

B4X:
....

Dim prevVol As Int
Dim curVol As Int
Dim volType As Int

If (StartingIntent.HasExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE") AND StartingIntent.HasExtra("android.media.EXTRA_VOLUME_STREAM_VALUE") AND StartingIntent.Action = "android.media.VOLUME_CHANGED_ACTION") Then
      prevVol = StartingIntent.GetExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE")
      curVol = StartingIntent.GetExtra("android.media.EXTRA_VOLUME_STREAM_VALUE")
      volType = StartingIntent.GetExtra("android.media.EXTRA_VOLUME_STREAM_TYPE")
Hi melamoud,

the code work properly when the phone screen is not OFF. If I turn off the screen, I could not make my service detect the volume key press.
Any solution to restart my app with volume key press?

Best regards,
 
Upvote 0
Top