Android Question Help with timer

apty

Active Member
Licensed User
Longtime User
I have the following code
B4X:
Sub timer1_Tick
Dim datetoday As String
    DateTime.DateFormat="M/d/yyyy HH:mm:ss"
    datetoday=DateTime.Date(DateTime.Now)

For i = 1 To 10
        WriteTextWriter(Ax&","&Ay&","&Az&","&datetoday)
    Next
End Sub

I would like the timer to write this values between 10am and 4pm every day. How can i loop using time?
 

stevel05

Expert
Licensed User
Longtime User
You would initialize and enable the timer to your required interval when the app starts. But if you want to start and end at a specific time each day, you should look at using a service which you can start at a specific time using StartServiceAt
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You should test the time value before enabling the timer, like

If time <16h00 and time >10h00 then timer.enabled=true else timer.enabled=false

This is just a pointer, additional logic is required in order to not re-fire the timer
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
How do you check if time is greater than? i tried the following but it gives me an error =>java.lang.NumberFormatException:invalid double: "07:38"
B4X:
lblStartSet.Text=txtStartTime.Text
    lblStopSet.Text=txtEndTime.Text

timer1.Initialize("Timer1", 5000)
Dim timenow,starttime As  Long
  starttime=DateTime.TimeParse(lblStartSet.Text)
  DateTime.TimeFormat="HH:mm"
  timenow=DateTime.Time(DateTime.Now)

  If starttime>=timenow Then
  timer1.Enabled = True
  End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
you are comparing a datetimevalue with a string (not a datetimevalue)...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How do i compare the dates? please help with sample code

no, i wont. You are here to learn.

as i wrote: you are comparing a datetimevalue (starttime) with a string (timenow)(not a datetimevalue)...
If starttime>=timenow Then

Think over what you have made different on the two and you should be able to find out the error you made...

timenow=DateTime.Now
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
i have tried with services but the service doesn't stop even if i call StopService("")
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Can you post an example that is not working?
 
Upvote 0
Top