Android Question Change brightness at a set time, how?

MikeSimpson

Member
Licensed User
Longtime User
Solved,
you will find a working sample with two set times to change the brightness of the screen in a 24h interval down on this page at post #17


Hello,

I need to change the brightness of the screen at about 6:30 PM to the minimum and at about 8:00 AM to the max.

I figured already out how to change the brightness settings, but I have no idea how I can set this automaticly at a set time.

Can someone please give me a sample code how to trigger something at a set time? Or at least tell me what Class and Methode to use?

If someone need a very simple sample to change the brightness, it is attached.
 

Attachments

  • Brightness Test.zip
    7.3 KB · Views: 195
Last edited:

MikeSimpson

Member
Licensed User
Longtime User
Thank you for the hint, but as a very beginner I find it very difficult to figure out the meaning of something like this:

StartServiceAt (Service AsObject, Time AsLong, DuringSleep AsBoolean)

So far I have figured out the code to change the brightness to low and to high with
B4X:
Dim phone1 As Phone

phone1.SetScreenBrightness(Max(0, 5) / 100) ' Set brightness low

phone1.SetScreenBrightness(Max(0, 5) / 100) ' Set brightness high

And then I have the code copied from thedesolatesoul copied from here: http://www.b4x.com/android/forum/threads/startserviceat-exactly-time.19191/#post-110507

B4X:
Dim tt as Long
tt = hours * DateTime.TicksPerHour + minutes * DateTime.TicksPerMinute

Dim NowDay As Long
NowDay = DateTime.DateParse(DateTime.Date(DateTime.Now))

Dim CurrentTime As Long
CurrentTime = DateTime.Now - NowDay

If tt > CurrentTime Then
    'Time has not passed, schedule for today
    NextRunTime = NowDay + tt
Else 'Schedule for tomorrow
    NextRunTime = NowDay + DateTime.TicksPerDay + tt
End If

Log("NextRunTime:" & NextRunTime & " Date:" & DateTime.Date(NextRunTime) &  "Time:" & DateTime.Time(NextRunTime))

Where I think,
B4X:
Dim NextRunTime As Long
is missing, because when past the code "NextRunTime" is not declared. Am I right or is there another reason?

My big problem now is to combine
B4X:
phone1.SetScreenBrightness(Max(0, 5) / 100)
with
B4X:
StartServiceAt (Service AsObject, Time AsLong, DuringSleep AsBoolean)

I have tried
B4X:
StartServiceAt("phone0.SetScreenBrightness(Max(0, 5) / 100)",NextRunTime,True)

StartServiceAt((phone0.SetScreenBrightness(Max(0, 5) / 100)),NextRunTime,True)

StartServiceAt(phone0.SetScreenBrightness(Max(0, 5) / 100),NextRunTime,True)

But this is probably very wrong. Sorry, but I am running in circles at the moment, every post I find is refering to the same post but non of them seems to explane the connection between StarServiceAt and what should be in between the " ".

I hope someon can help me.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Yes, I went thru it, maybe my English is not good enough to understand it fully. I have also tried to put code of the brightness in a service module named "brightness_low" and then tried
B4X:
StartServiceAt(brightness_low,NextRunTime,True)
but didn't work. I still can't figure out where to put the code in order that it is working.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
OK, here is what I came up with, unfortunatly not working. I have the zip file attached.
After the button is clickt, the service modul should start and set the brightness of the screen to the minimum at the time set in hours and minutes variables in the Process_Globals of the Service Modul.

Activity Modul (only one button click to start the service modul):

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

    Private btnStartService As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("Main")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub btnStartService_Click

    StartService(brightness_time)   
   
End Sub

The Service Modul with the time, to set be first in the code in hours and minutes:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim phone1 As Phone
   
    Dim hours As Long
    hours = 13                    'Set houre when the service should be started, 1 for 01:00am, 13 for 01:00pm etc.
   
    Dim minutes As Long
    minutes = 46                'Set the minute of the houre when the service should be started, 0 - 60
   
    Dim NextRunTime As Long
   
    Dim tt As Long
    tt = hours * DateTime.TicksPerHour + minutes * DateTime.TicksPerMinute

    Dim NowDay As Long
    NowDay = DateTime.DateParse(DateTime.Date(DateTime.Now))

    Dim CurrentTime As Long
    CurrentTime = DateTime.Now - NowDay
   
   
End Sub


Sub Service_Create

    If tt > CurrentTime Then
    'Time has not passed, schedule for today
    NextRunTime = NowDay + tt
Else 'Schedule for tomorrow
    NextRunTime = NowDay + DateTime.TicksPerDay + tt
End If

    Log("NextRunTime:" & NextRunTime & " Date:" & DateTime.Date(NextRunTime) &  "Time:" & DateTime.Time(NextRunTime))

    StartServiceAt("",NextRunTime,True)
   
   
End Sub


Sub Service_Start (StartingIntent As Intent)

    phone1.SetScreenBrightness(Max(0, 5) / 100)    ' Will set the screen brightness to (0) minimum or maximun (100)

End Sub

Sub Service_Destroy

End Sub

Where is the misstake?
 

Attachments

  • Brightness_Time_Test.zip
    8.1 KB · Views: 167
Upvote 0

mangojack

Expert
Licensed User
Longtime User
SetScreenBrightness
Description ... Sets the brightness of the current activity.
Value - A float between 0 to 1. Set -1 for automatic brightness.

** This method cannot be called from a service module.

You might have to declare ' Phone1 ' in your main activity and call this method from there.
then get the service to start the Activity when needed.

also remove all code from Service Process_Globals and Service_Create
place it all in Service_Start . Basically ...
B4X:
Sub Service_Start (StartingIntent AsIntent)
  'Declare all your Variables ... Hours,Mins,TT etc
  'Calculate your next service start time
  'Set your next service start time

  'start your activity to do you screen dimmy thing
  StartActivity("Main")

  'stop the service if you wish ?
End Sub
 
Last edited:
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Thank you for the hint, but where I have to place the code to change the brightness in the Main Activity Modul?
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
personally .. when I use a service to start an activity to perform a small task, once the task is done i close the activity.
saying that I would put the method in Activity_Create.
I believe in your case Activity_Resume would also work.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
If I put the code for the brightness into Activity_Create or Activity_Resume, it will change the brightness as soon as I start the programm, but the Activity to change the brightness should only be triggered when the time has come.

So I still need the connection between:
B4X:
StartServiceAt("",NextRunTime,True)
in the Service Module
and
B4X:
phone1.SetScreenBrightness(Max(0, 5) / 100)
somewhere in the Main Activity Module.

If I for example put the brightness code in a new Sub like:
B4X:
Sub change_brighthness

      phone1.SetScreenBrightness(Max(0, 5) / 100)

End Sub

and then "call" this Sub after StartServiceAt? Is this possible and if so how?

Edit: The service should run "forever". My phone should be on 24h but in the night should be darker to save battery.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Is there is a reason that your phone display should be on 24hr ? .. rather than just sleep mode after so many minutes? .. that will save the battery.

anyway .. In the Service module you could set a flag to indicate the Activity was started by the service . Only if it was true would you run your sub to dim the screen.

B4X:
Sub Process_Globals
  Public flagServiceStart As Boolean
End Sub

Sub Service_Start (StartingIntent As Intent)
  flagServiceStart = True
  StartActivity("Main")
'................

'in the Activity module

If brightness_time.flagServiceStart = true Then
  'call your DimBrightness sub ...

but surely .. if all you are doing is automatically dimming the screen at night and brightening it in the morning ,then once this is all running correctly it is only the very first run , and also reboot occasions that you would have to manually adjust brightness. ?

ie: very first run ... start app > start service > set service restart > restart app > stop service > change brightness (manually readjust your brightness)

.. after that all future brightness adjustments should be welcomed ? .. Or am I missing something ?
 
Last edited:
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Yes, I want to use my old Android Phone as a WiFi door bell. (Other wireless doorbells I have tried arnÄt working properly) Outside I don't have a power supply, so I want to charge the battery with solar power during the day. At night time it is enghouf to run the display at a minimum, otherwise the battery would not last the night. The door bell part is already working, now I need to add the part to save energy.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
Looks like I just cam up with a solution.
The first thing I discovered is, that aperrently as soon as I start the service, it will trigger on time iven if the time didn't came, but after that, it looks as it will trigger every 24 h.

So my solution is this, for two services, one in the evenig and one in the mornig.

1. I create two Service Modules with the two different times.

2. In the Main Activity Modul I create two Sub, one to set lower brightness one for full brightness.

The code I use to call the brightness is after each StartServiceAt is:
B4X:
CallSub(Main, "Dimmer_Screen")
and
B4X:
CallSub(Main, "Full_Screen")

So I don't use StartActivity("Main")

Then the Sub Service_Start keeps clean and maybe I don't have to add the "flagServiceStart", I will see, after 24 hours ;-)

Thank you mangojack, I think I have solved the problem. I will try tomorrow to write a clean example of my solution, maybe it will help someone else.
 
Upvote 0

MikeSimpson

Member
Licensed User
Longtime User
That was faster then I expected.

Attached you will find my working example to set the brightniss of the screen automaticly to low and high at a set time in an interval of 24 h.
Click the Start button to start the services and the three other buttons to set the brightness manualy.
You do need to call StartActivity if the activity is not visible. Otherwise CallSub will not do anything.

Thank you Earl for the warning, I will keep it in mind for the future, as for my current project CallSub will do.
Just out of curiosity, would it work first to call StartActivity and then CallSub?

This way I could keep the Sub Service_Start "cleen" and would not have to deal so much with Global variables and If ... Then.
 

Attachments

  • Brightness_Time_Test.zip
    9.2 KB · Views: 209
Upvote 0
Top