How do you wake up an Android device for a period of time?

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Can you tell me how to make an app that will turn the Android device on or at least keep it in a wake state only while a sound is being played in full?

I ask this because I have an Asus Transformer Android tablet and the firmware has a bug in it that makes the sound turn on and off only if the unit is asleep or on standby. I don't have this problem if the USB cable is plugged in or the unit is currently on.

That's why I would like to make a service app that will constantly look to see if any other app is playing a sound so I can let those other apps play the sound completely.
 

rleiman

Well-Known Member
Licensed User
Longtime User
I was thinking about making a service that will keep an eye out for when the media player starts to play a sound.

I don't really know Java but found this that I think somehow detects when a sound has started playing and also when it stops:

android.media.AudioManager.OnAudioFocusChangeListener

Can B4A do something with this?

Maybe there's a way to wake up the Android device from within the service.

Thanks.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Can you show me what to do so I can create a service that will run on the hour, 1/4, 1/2 and 3/4 past each hour?

I would also like to have it stay awake for a period of 1 minute when it wakes up.

Thanks so much for all you help in the past.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It will be simpler to schedule your service to run every 15 minutes.
In that case you should call:
B4X:
StartServiceAt("", DateTime.Now + 15 * 60 * 1000, True)
This should be in Sub Service_Started.

In order to keep the device awake you will need to use PhoneWakeState and a timer that will release it after one minute. You should use PartialLock and ReleasePartialLock.

Note that not all devices behave the same when it comes to sleeping states.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Thanks for the code.

Should this code be placed inside the service so it can be called over and over again when each 15 minute period has passed?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

I started to build it but I think I'm missing something to wake up the Android device.

Here is the activity that starts the service:
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)

   StartServiceAt(DeviceWakupService, DateTime.Now, True)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

And here's the service. I set it up so it will call itself every 15 seconds so I know at least for now it running every 15 seconds:
B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim pw As PhoneWakeState
   
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
   
   ToastMessageShow("At Device Wakeup", True)

   StartServiceAt("", DateTime.Now + .25 * 60 * 1000, True)

   pw.KeepAlive(True)
   
End Sub

Sub Service_Destroy

End Sub

It runs every 15 seconds because I can see the toast message only when the Android device is turned on but when I turn off the screen and that puts it into standby/sleep mode it does not wake up when the StartServiceAt statement is executed.

What else am I missing to get it to wake up the Android device?
 
Upvote 0
Top