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.
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.
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?
You will not be able to turn on the screen. You should instead use PartialLocks and only keep the CPU running.
Add Log(DateTime.Time(DateTime.Now)) to see that it runs.