Android Question Clock -> Button

G-ShadoW

Active Member
Licensed User
Hello, how can I execute command from Mon-Sat from 07:00AM till 23:00PM

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

    Dim lblClock As Label
    Dim timClock As Timer 
    Private Button1 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("0")
    lblClock.TextSize = 40
    lblClock.Text = "00:00:00"
    DateTime.TimeFormat = "HH:mm:ss"  
    timClock.Initialize("timClock",1)
    timClock.Enabled = True
   
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub timclock_Tick
    'DateTime.TimeFormat = "hh:mm"
    lblClock.Text = DateTime.Time(DateTime.Now)

End Sub

Sub Button1_Click
' ?
End Sub
 

Peter Simpson

Expert
Licensed User
Hello,
I would use a Sticky Service running in the background.
B4X:
#StartCommandReturnValue: android.app.Service.START_STICKY
Your service would have to be set to this.
B4X:
#StartAtBoot: True.
Once your service has started you will need to have in Sub Service_Start StartServiceAt and set DuringSleep to True. I personally do not use Timers for long running tasks as I find them too unreliable. Your StartServiceAt will probably look something like this.
B4X:
StartServiceAt(Null, DateTime.TicksPerSecond, True) 'Restart the service every 60 seconds. I personally restart every minute on the dot.
You also ask the following, but you do not state how often you want to check in between 07:00AM an 23:00PM. Every x (how often)?
execute command from Mon-Sat from 07:00AM till 23:00PM
 

G-ShadoW

Active Member
Licensed User
Sorry I didnt properlly ask a question,
what I want need is when user press a button to check what is today (Date) and (Time)
if it's for example (Monday) and time is example 09:30 the command will execute...
if it's (Monday) and the time is 23:35 then he can't execute command...
So from (Mon-Sat) from 07:00 till 23:00 command will work and from 23:00 till 07:00 he will not be able to execute command.

B.R.
 

Peter Simpson

Expert
Licensed User
What is it you are going to execute and why does the user need to to it manually?

You may not need to do this but I would remove the ":" whan formatting the time and then compare the time using < and >, compare it with 070000 and 230000. Sorry I'm answering this using my phone so I have my laptop to hand for better code example.
 

G-ShadoW

Active Member
Licensed User
I have try something, but it's stupid, cant work like this...


B4X:
Sub Button1_Click
Dim 23 As String
Dim 07 As String
Dim now As Long = DateTime.now
DateTime.TimeFormat = "HH:mm:ss"
EditText1.Text= DateTime.Time(DateTime.Now)
EditText1.Text=EditText1.Text.Replace(":", "")

23 = "230000"
07 = "070000"

If 07 > EditText1.text Then

   '
   Else
   '
End If

If 23 > EditText1.text Then
   '
   Else
   '
End If
End Sub
 

G-ShadoW

Active Member
Licensed User
Hey, so the user presses the button and it tells them to stop working?

What service?

I'm completely lost :confused:
Ok, imagine that you want to order "pizza" and working time is from 07:00 till 23:00, and if you want to call afther 23pm, when you press button it will tell you that working time is over, for example... :)
 

G-ShadoW

Active Member
Licensed User
I know it's a messy code, but it works like I want to...
If someone can post simple code that do exactly the same?

B.R.

B4X:
Sub Button1_Click
Dim hour As String
Dim date As String
DateTime.TimeFormat = "HH"
hour=DateTime.Time(DateTime.Now)
date=DateTime.GetDayOfWeek(DateTime.Now)

If date = "2" OR date = "3" OR date = "4" OR date = "5" OR date = "6" OR date = "7" Then ' date from monday till saturday
If hour = "23" OR hour = "01" OR hour = "02" OR hour = "03" OR hour = "04" OR hour = "05" OR hour = "06" Then

' from monday till saturday you cant use command call
Else
' yes you can use commad

End If
End If

If date = "1" Then ' it's sunday opening time is from 13:00 PM till 23:00 PM
If hour = "23" OR hour = "01" OR hour = "02" OR hour = "03" OR hour = "04" OR hour = "05" OR hour = "06"  OR hour = "07" OR hour = "08" OR hour = "09" OR hour = "10" OR hour = "11" OR hour = "12"  Then
' it's sunday and you cant use command call
Else
' it's open, you can use cammand call
End If
End If

End Sub
 
Top