Power management & Screen brightness

refsmmat

Member
Licensed User
Longtime User
Hello all,

I have an application that needs the screen operation periodically (20 seconds or so every 8 minutes. The application works successfully using "Phone2.Keepalive(True)", but this holds the screen on continuously and, after an hour or so, just about melts the device. I am running the phone on an extrenal supply, but the battery is still depleted since the current draw is so high.

I have been experimenting with ways to 'wake' the Activity and manipulate the screen brightness by using "Keepalive" and setting the screen brightness with "SetScreenBrightness". This works poorly. "Keepalive" should keep the phone from locking, but setting a zero screen brightness yields a state where it is no longer possible to wake the activity. A screen brightness of 0.1 is still quite bright (& hot after a while) .

I think I need to look at another method - effectively zeroing the screen brightness until either the periodic task starts or if the screen is touched.

I have attached sample code (servicetest_powercontrol) I am using to explore this. It calls a sub (callsub) in the main every 30 seconds.

I welcome any suggestions on improving this.

I am considering using reflection as there seems to be positive commentry about controlling the screen in this way, but I don't really want to necessitate changes in the manifest to accomodate the screen control.


Many thanks,

refsmmat
 

Attachments

  • servicetest_powercontrol.zip
    8.6 KB · Views: 383

refsmmat

Member
Licensed User
Longtime User
Do you release the lock after 20 seconds?

Hi Erel,
I am not sure what you mean by lock. The screen lock is becoming active while the test program is running.

I have been forcing the screen to stay active with "keepalive". At the same time I set the brightness to 0, I also used "Phone2.ReleaseKeepAlive".
Using keepalive seemed to have no effect as "SetScreenBrightness(0)" did not seem recoverable. The test code (attached to the orig post) initiated "keepalive" and "SetScreenBrightness(1)" about 30 sec after "SetScreenBrightness(0)", but this had no effect.
The home button revealed the phone had entered screen lock with zero brightness.

Regards,
refsmmat
 
Upvote 0

refsmmat

Member
Licensed User
Longtime User
Hi Erel,
Thanks for the reply.
ReleaseKeepAlive works & allows the screen to sleep, but keepalive does not bring it back to illumination. I suspected the activity was paused, but I am running the timer in a service. As well, the keepAlives are in s subroutine in Activity which is called from a service module using "callsubdelayed".

I have tried a similar trick to periodically sleep and wake the screen using screenbrightness(0) and (1) or (-1) (executed from a sub in Activity which is called from the service module) but I have the same problem.

Irrespective of the keyguard timer (currently 30min) if I execute screenbrightness(0) I can't wake the screen using screenbrightness(1) and I find (by pressing home key) that the keyguard always becomes active if screenbrightness(0) is executed. Once I unlock the keyguard I find the application is still running - or at least it is resuming.

Is there an option I am overlooking?
I appreciate your help.
Regards,
refsmmat
 
Upvote 0

refsmmat

Member
Licensed User
Longtime User
Thanks again Erel,

Assuming I have started from a dark screen (kelock timer set to >10min)

If, from a service module, I 'callsubdelayed' a sub in the Activity module which contains keepalive, it should:

1. Wake up the activity (sorry, Activity_Resume)
2. Execute keepalive within the now awake Activity module - and screen illumination should result.

I will redo my test code to see if I can get this outcome. If it is still unsuccessful I will post it & see if any problems are visible.

Regards,
refsmmat
 
Upvote 0

refsmmat

Member
Licensed User
Longtime User
Hi Erel (et al)

I have made a simple test program for the screen brightness.
It is not working as i would expect. The starting condition is the screenlock > 10 minutes.
The result is no change in screen brightness.
The second test had the screenlock set to 30 sec. For this case, the screenlock occurs after the sleepsub is initiated, but keepalive does not wake it.

Are there any suggestions around the commands used - or my implementation?
Thanks,
refsmmat


Here is the activity;
B4X:
Sub Process_Globals
   
End Sub

Sub Globals

   Dim Phone1 As Phone
   Dim Phone2 As PhoneWakeState
End Sub

Sub Activity_Create(FirstTime As Boolean)

StartService(logtime)
End Sub

Sub Activity_Resume
ToastMessageShow("activity resume",True)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
ToastMessageShow("activity pause",True)
End Sub

Sub callbackawake
ToastMessageShow("Awake sub called", False)
'Phone1.SetScreenBrightness(-1)
Phone2.KeepAlive(True)
End Sub 

Sub callbacksleep 
ToastMessageShow("Sleep sub called", False)
Phone2.ReleaseKeepAlive
'Phone1.SetScreenBrightness(0)
End Sub

Here is the service:
B4X:
'Service module
Sub Process_Globals

Dim Timer1 As Timer
Dim counter As Int
Dim switch As Boolean : switch = True
Dim Phone2 As PhoneWakeState
   Dim Phone1 As Phone

End Sub
Sub Service_Create
Timer1.Initialize("Timer1", 90000 ) 
Timer1.Enabled = True
End Sub

Sub Service_Start (StartingIntent As Intent)

ToastMessageShow("timer service started",True)
End Sub

Sub Service_Destroy

End Sub

Sub Timer1_Tick 

If switch = True  Then
CallSubDelayed(Main, "callbackawake")
switch  = False
Else
CallSubDelayed(Main, "callbacksleep")
switch  = True
End If

End Sub
 
Upvote 0

refsmmat

Member
Licensed User
Longtime User
Hi Erel,
Unfortunately, I am still not able to get this to work successfully.
With the code below, the application goes to a keyguard lock state if keyguard lock timer is 30 sec. It does not return from this state without unlocking the keyguard manually.

If keyguard lock is 10 to 30minutes, the screen does not dim - this is expected as keyguard lock timer is 30 minutes.



I have added a different approach to this - using screenbrightness(0) to blank the screen. This also results in a black screen, but also a keyguard lock - irrespective of the keyguard lock timer (it was 30min for the test above).

FURTHER TO COMMENTS ABOVE:

I have found using screenbrightness(0) engages the keyguard & pauses the activity - even when keepalive is used. Is there a way to blank the screen without engaging the keyguard?





B4X:
Sub Timer1_Tick 

If switch = True  Then
StartActivity(Main)
CallSubDelayed(Main, "callbackawake")
switch  = False
Else
StartActivity(Main)
CallSubDelayed(Main, "callbacksleep")
switch  = True
End If

End Sub

Also, I have attached the sleep and awake subroutines in main:

B4X:
Sub callbackawake
ToastMessageShow("Awake sub called", False)
'Phone1.SetScreenBrightness(-1)
Phone2.KeepAlive(True)
End Sub 

Sub callbacksleep 
ToastMessageShow("Sleep sub called", False)
Phone2.ReleaseKeepAlive
'Phone1.SetScreenBrightness(0)
End Sub


I have tried a more extreme solution - using keepalive and screenrightness(0)
together. This worked poorly as I needed to use the power button to get the display back to normal brightness.

I suspect a contibuting factor is the activity itself is pausing during the execution.

Any further advice would be very helpful.

Thanks,

refsmmat
 
Last edited:
Upvote 0
Top