Alexandre Pires 0 Posted October 28, 2020 Hi Guys, I need my Android background service to read a table from my database every 30 seconds. What would be the best way to do this? I am currently using an infinite loop in a thread and sleep, but I believe it is not the best approach. @Dave Nottage Is there any kastri component that helps to do this? I am using Delphi 10.3 Thanks for some help Alexandre Share this post Link to post
Remy Lebeau 1394 Posted October 28, 2020 Why not a simple timer instead of a sleeping loop? Share this post Link to post
Alexandre Pires 0 Posted October 28, 2020 @Remy Lebeau do you mean ttimer component? But does ttimer work on services? Share this post Link to post
Rollo62 536 Posted October 29, 2020 13 hours ago, Alexandre Pires said: @Remy Lebeau do you mean ttimer component? But does ttimer work on services? No, not in background. But in background you need maybe also other measures to keep Android awake. Share this post Link to post
Moskw@ 0 Posted November 30, 2020 Hello, do not use sleep in background services, Android can think that Your service is not working (especially in energy save mode) and try to kill. Instead of this use for example : const cTimeInterval = 30000; var aLastTime : TDateTime; begin aLastTime := Now(); // use aLastTime := Now()-1; (whatever to start imiditly) while true do begin if (MilliSecondsBetween(Now(),aLastTime)>=cTimeInterval) then begin aLastTime := Now(); // do somethink there end; end; end; if You need to have time interval counting after to do something move aLastTime := Now(); after work you need to make. For me works better if I create that in new Thread in service. while true do - you can change to do somethink to brake loop if you need best regards Moskw@ Share this post Link to post