Android Question Prevent screen from sleep while playing video

aeric

Expert
Licensed User
Longtime User
I refer to the following sample code:
How do I prevent screen timeout

I am using ExoPlayer library. When the video plays until 2 minutes, the screen turns dim and then screen lock.

I have tried to add the permission in manifest but the screen still sleep.

Method 1:
B4X:
AddPermission(android.permission.WAKE_LOCK)

Method 2:
B4X:
AddManifestText(<uses-permission android:name="android.permission.WAKE_LOCK" />)

I am using Xiaomi Redmi 5 Plus (Android 7.1.2). Is there any setting I need to change in my phone?
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
You need the "phone" library and then use these examples.

B4X:
    Dim ps As PhoneWakeState
    ps.KeepAlive(True) '<-- Keeps the screen on at set brightness
   
    ps.ReleaseKeepAlive '<-- Releases the above command and allows the phone to sleep again

Enjoy !!

EDIT:
I've just seen that you have done this and that it still sleeps after two minutes - it should not - but there must be a work around - I think the Os is detecting inactivity and forcing a closure - Anyone else with a work around ?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You need the "phone" library and then use these examples.

B4X:
    Dim ps As PhoneWakeState
    ps.KeepAlive(True) '<-- Keeps the screen on at set brightness
   
    ps.ReleaseKeepAlive '<-- Releases the above command and allows the phone to sleep again

Enjoy !!
Yes, I follow the code as Erel shared.
B4X:
Sub Process_Globals
   Dim pw As PhoneWakeState
End Sub
Sub Activity_Pause (UserClosed As Boolean)
   pw.KeepAlive(True) 'set false if brightness is not needed
End Sub
Sub Activity_Resume
   pw.ReleaseKeepAlive
End Sub
 
Upvote 0

npsonic

Active Member
Licensed User
I don't own Xiaomi and can't test this, but there is possibility that Xiaomi just doesn't keep screen on, no matter what you do.
It completely depends what manufacturer decides to do and how they modify android.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I don't know if I am doing wrong with the following code but it works for me and make more sense .

B4X:
Sub Activity_Resume
    pw.KeepAlive(True) 'set false if brightness is not needed
    player1.Position = pos
    player1.Play
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    pw.ReleaseKeepAlive
    pos = player1.Position
    player1.Pause
End Sub
 
Upvote 0
Top