Android Code Snippet Prevent Phone From Sleeping

JohnD

Active Member
Licensed User
Description: Prevent the phone from going to black energy saving screen

Select the Phone Library from the [Libs] tab in the IDE

phone.PNG


Add the following in Sub Globals of the Activity you want to keep awake.

B4X:
Sub Globals

Dim pw As PhoneWakeState
         
End Sub
Put the Stay awake code in Sub Activity_Resume of the same Activity. For me I only need the Activity to stay awake if the user is taking a timed test.

B4X:
Sub Activity_Resume

If (comp.gbTimer = True) Then
    pw.KeepAlive(True)
End If

End Sub
Make sure you Turn Off the keep awake stake so you don't waste the battery. The place to do this in Sub Activity_Pause

B4X:
Sub Activity_Pause (UserClosed As Boolean)

If UserClosed Then
    pw.ReleaseKeepAlive
End If

End Sub
Tags: Phone Library, PhoneWakeState, Android OS Function
 
Last edited:

stevel05

Expert
Licensed User
Thanks for posting John, could you add a Tags: line to the post so it is boosted in the forum search with the rest of the code snippets.
 

JohnD

Active Member
Licensed User
How is comp declared in activity resume?
comp isn't declacred in activity resume. gbTimer is a global declared in the comp code module.
B4X:
'Module comp
Sub Process_Globals
   Dim gbTimer = False As Boolean
End Sub
If the user is taking a timed test (which is optional), comp.gbTimer = True, else comp.gbTimer = False and device is allowed to sleep. Hope this helps, JD.
 

merlin2049er

Well-Known Member
Licensed User
Hmm, ok. I need to figure out if my phone / tablet falls asleep is my app still running in the background?
I've got a bluetooth low energy monitoring app, which connects to a ble device, and updates a sqlite table.
 

JohnD

Active Member
Licensed User
Hmm, ok. I need to figure out if my phone / tablet falls asleep is my app still running in the background?
I've got a bluetooth low energy monitoring app, which connects to a ble device, and updates a sqlite table.
In my code the the user selects the timed test option. Then I test for comp.gbTimer = True. If the test is being timed, don't sleep. You could do something like comp.gbIsRunning = True at the beginning of your processing algorithms. Set comp.gbIsRunning = False at the end of all your processing. In Sub Activity Resume:
B4X:
Sub Activity_Resume
If (comp.gbIsRunning = True) Then
 pw.KeepAlive(True)
EndIf
End Sub
 

Informatix

Expert
Licensed User
You can also change the flag Keep_Screen_On of an activity with the reflector library if you don't want to embed the Phone lib in your app:
B4X:
Sub KeepScreenOn(IsEnabled As Boolean)
    'Prevents the screen from sleeping when the current Activity is in the foreground
    Dim r As Reflector
    r.Target = r.GetActivity
    r.Target = r.RunMethod("getWindow")
    Dim FLAG_KEEP_SCREEN_ON As Int = 128
    If IsEnabled Then
        r.RunMethod2("addFlags", FLAG_KEEP_SCREEN_ON, "java.lang.int")
    Else
        r.RunMethod2("clearFlags", FLAG_KEEP_SCREEN_ON, "java.lang.int")
    End If
End Sub
 

Informatix

Expert
Licensed User
In this page, Google gives some useful advices about keeping the device awake. Following their advices, you should avoid using the code in post #1 for an activity (from their page: "you should never need to use a wake lock in an activity") and use instead the code that I posted above. Moreover, my code needs no permission, contrary to PhoneWakeState.
The code in post #1 is recommended for a service.
 
Last edited:

merlin2049er

Well-Known Member
Licensed User
Thanks. What I really need to test is if my app continues to run and log data from ble even if the screen goes dark.
 

merlin2049er

Well-Known Member
Licensed User
hmm, basically I enable notify on the bluetooth low energy characteristics I'd like to monitor. The hardware pushes out values every 10 seconds or so, I update the stats and insert into a sqlite table.

So, your saying that all apps stop running when the screen goes dark?
 

Informatix

Expert
Licensed User
There are two states to distinguish: when the screen goes dark, which comes first, and when the device goes to sleep. When the latter happens, the OS raises the onPause event of your Activity. Then your Activity stops running. Before that, the screen can be off while the app is still running. To keep it running and prevents the device from sleeping, you have to acquire a partial wake lock. You could do that with your Activity but that's not the purpose of an activity. A service is recommended here, especially when you want to monitor something. Remember that your activity will be stopped if you receive a phone call while a service will continue to run and receive data.
 

RFman

New Member
Licensed User
Informatix,
In this regard, is there a list somewhere of all the possible events that may send an app to pause?
RFman
 

a2stepper

Member
Licensed User
i have used the keep awake and works great but i'm using a media player with mabey an hour audio file playing and would
want the screen to turn off but keep the app alive and playing.
is there any way to do this?

thanks.
paul
 
Top