Android Question waking the phone from volume buttons

aeropic

Active Member
Licensed User
Longtime User
Hi All,

I would want to be able to access to the physical volume buttons from a locked device...
I already succeed to access to a pseudo state of the volume buttons from a service using this code:

in the manifest added an intent to my service:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>  '<<<<<<<< USELESS
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.


AddReceiverText(TestService, <intent-filter>
    <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>)

and my Testservice code is:
B4X:
Sub Process_Globals
  Dim PE As PhoneEvents
  Dim Vibrate As PhoneVibrate ' For phone vibration
End Sub

Sub Service_Create
  PE.Initialize("PE")
End Sub


Sub Service_Start(startingIntent As Intent)
  Dim mystring = startingIntent.ExtrasToString
  Log(mystring)
      Dim  volume As Int = startingIntent.GetExtra("android.media.EXTRA_VOLUME_STREAM_VALUE")
      Dim  volume_prec As Int = startingIntent.GetExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE")
      Dim delta As Int
    
      delta = volume - volume_prec
    If delta > 0 Then
        Vibrate.Vibrate(100)
    Else If delta <0 Then
        Vibrate.Vibrate(50)
    Else If (delta = 0 AND volume = 0) Then
        Vibrate.Vibrate(50)
    Else If (delta = 0 AND volume = 7) Then
        Vibrate.Vibrate(100)
    End If

End Sub

When the phone is unlocked, the background service catches the intent and performs the correct action when volume is changed but when locked, nothing ... except if the media player is reading a mp3, in that case in addition to change the volume, the vibrations are present

Do you know what to add to allow my app to act as a media player with volume buttons remaining active even when locked ?

thanks
Alain
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The android.media.VOLUME_CHANGED_ACTION is an internal, hidden API. It is supposed to only be used by system processes. This means that we cannot know how it should behave.

Media player probably acquires a partial lock to prevent the CPU from sleeping. Have you tried to acquire such a lock (though it will eventually drain the batter)?
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
The android.media.VOLUME_CHANGED_ACTION is an internal, hidden API. It is supposed to only be used by system processes. This means that we cannot know how it should behave.

Media player probably acquires a partial lock to prevent the CPU from sleeping. Have you tried to acquire such a lock (though it will eventually drain the batter)?

Thanks Erel, Yes I tried a partial lock through the usage of FLAG_SHOW_WHEN_LOCKED and FLAG_KEEP_SCREEN_ON. I think it's equivalent to a partial lock isn't it ?

That's weird Android does not give access to physical buttons from services... From your (deep) knowledge of Android, do you know if it exists any other official event that is triggered by volume up and down buttons presses ?
 
Upvote 0

KY Leng

Member
Licensed User
Longtime User
The android.media.VOLUME_CHANGED_ACTION is an internal, hidden API. It is supposed to only be used by system processes. This means that we cannot know how it should behave.

Media player probably acquires a partial lock to prevent the CPU from sleeping. Have you tried to acquire such a lock (though it will eventually drain the batter)?
Dear Erel,
I also try with :
PhoneW As PhoneWakeState
PhoneW.PartialLock

But nothing change. Even if I run media player, I can only see the intent is detected in the log, but my app could not start unless I unlock the screen.

My code in the service is like this:

If (StartingIntent.Action = "android.media.VOLUME_CHANGED_ACTION") Then
Log("> Unlock from Intent")
If IsPaused(Main) = True Then
StartActivity(Main) 'Start Main Activity
End If
If Main.LinkConnect = False Then
CallSubDelayed(Main,"Connect2DeviceMACAdd")
End If
Main.StrOut = "$KhInn,PID,U" & CRLF
End If
 
Upvote 0
Top