Hi All,
I can not figure out why my App doesn't start at the scheduled date/time, certainly I am doing something wrong but can not spot it! I do not have the application stopped and I do not force stop the application from the settings, and also on max performance with no limitation for background tasks but for some reason it still doesn't start and the date and time specified when I press on the home button or close the app - it works if the app is in the foreg.
I have a DB that I write the schedule to in the main activity when the user select the date and time and duration, the data is saved to the DB then it is sorted and then I use the StartServiceAtExact:
then from a service I have the code below which shall remove the entry from the DB then add 1 day and then save it to the DB so it will be scheduled for tomorrow:
I can not figure out why my App doesn't start at the scheduled date/time, certainly I am doing something wrong but can not spot it! I do not have the application stopped and I do not force stop the application from the settings, and also on max performance with no limitation for background tasks but for some reason it still doesn't start and the date and time specified when I press on the home button or close the app - it works if the app is in the foreg.
I have a DB that I write the schedule to in the main activity when the user select the date and time and duration, the data is saved to the DB then it is sorted and then I use the StartServiceAtExact:
B4X:
Starter.myCUR = Starter.mySQL.ExecQuery("SELECT rowid, Sched_Type, Date,Time,Duration,Topic,DevName FROM Schedule ORDER BY Date,Time LIMIT 1")
For i = 0 To Starter.myCUR.RowCount - 1
Starter.myCUR.Position = i
Dim d_schedule As String= Starter.myCUR.GetString("Date")
Dim t_schedule As String= Starter.myCUR.GetString("Time")
Next
CancelScheduledService(Sched_db_sort)
StartServiceAtExact(Sched_db_sort,DateTime.DateTimeParse(d_schedule,t_schedule),True)
End Sub
then from a service I have the code below which shall remove the entry from the DB then add 1 day and then save it to the DB so it will be scheduled for tomorrow:
B4X:
Sub Service_Start (StartingIntent As Intent)
Starter.myCUR = Starter.mySQL.ExecQuery("SELECT rowid, Sched_Type, Date,Time,Duration,Topic,DevName FROM Schedule ORDER BY Date,Time LIMIT 1")
For i = 0 To Starter.myCUR.RowCount - 1
Starter.myCUR.Position = i
rowid = Starter.myCUR.GetString("rowid")
sched_type = Starter.myCUR.GetString("Sched_Type")
d_schedule = Starter.myCUR.GetString("Date")
t_schedule = Starter.myCUR.GetString("Time")
sched_duration = Starter.myCUR.GetString("Duration")
topic_schedule = Starter.myCUR.GetString("Topic")
sched_devname = Starter.myCUR.GetString("DevName")
Next
Dim temp() = Conv.StringToBytes(pub_value_ON,"UTF8") As Byte
Starter.mqtt.Publish2(topic_schedule,temp, 2,True)
mytimer.Initialize("durationSched", sched_duration *60*1000)
mytimer.Enabled = True
Service.StopAutomaticForeground
End Sub
Sub durationSched_Tick
Dim temp() = Conv.StringToBytes(pub_value_OFF,"UTF8") As Byte
Starter.mqtt.Publish2(topic_schedule,temp, 2,True)
Starter.query = "DELETE FROM Schedule WHERE rowid = ?"
Starter.mySQL.ExecNonQuery2(Starter.query,Array As String(rowid))
Select sched_type
Case "Daily"
x = DateTime.Add(DateTime.DateTimeParse(d_schedule,t_schedule),0,0,1)
d_schedule=DateTime.Date(x)
t_schedule=DateTime.Time(x)
Starter.Query = "INSERT INTO Schedule VALUES (?, ?, ?, ?, ?, ?)"
Starter.mySQL.ExecNonQuery2(Starter.Query, Array As String(sched_type,d_schedule,t_schedule,sched_duration, _
topic_schedule,sched_devname))
Dim n As Notification
n.Initialize2(n.IMPORTANCE_HIGH)
n.Icon = "icon"
n.AutoCancel = True
n.Vibrate = False
n.Sound = True
n.SetInfo2("New schedule","Next Scheduled for: "& d_schedule & "at" & t_schedule , "odNotifica", Main)
n.Notify(1)
StartServiceAtExact(Me,DateTime.DateTimeParse(d_schedule,t_schedule),True)
StopService(Me)
End Sub