Android Question [Solved] StartServiceAtExact doesn't start at the given date/time

AymanA

Active Member
Licensed User
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:

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
 

AymanA

Active Member
Licensed User
Hi Erel, thank you so much for your reply!

Yes the notification appears with the correct tomorrow's date and time but only when the app is in foreground.

Yes sir I am running in release mode.

Sure will remove the StopService and CancelScheduledService and will let you know.
 

Attachments

  • Screenshot_20191021-145541.jpg
    Screenshot_20191021-145541.jpg
    37.2 KB · Views: 99
Upvote 0

AymanA

Active Member
Licensed User
Hi Erel, I have removed the StopServie and the CancelScheduledService and now my application behaves as below:

If I press on home button It works
upload_2019-10-22_11-20-33.png


When I press on home and then close the recent apps (left button then close all on Samsung galaxy note FE (note 7) - I think this is the close recent apps) or close the recent app directly my application doesn't start:

upload_2019-10-22_11-22-22.png


Is this the default behavior? as I need to let my app works even if a user close the recent apps.
 
Upvote 0

AymanA

Active Member
Licensed User
Ok perfect thank you so much for your help, really appreciate it.

One more question if you do not mind, i have tried the push notification FCM and it is indeed a better option but my problem with it is that there is delay in receiving the messages (can be fee minutes to never receive the notification at all), so is this the normal behaviour? If not and it shall be instantaneous then i will be happy to open a new thread to show what is happening.
 
Upvote 0
Top