Delphi 10 Seattle Background Service And Threads
Solution 1:
There's no need to put your DoNotification in a thread. Just setup a TTask with an eternal loop in your AndroidServiceCreate, and then check the elapsed time, and when it's passed, call the DoNotification then.
T := TTask.Run (procedure
begin
TimeNow := Now;
Count := 0;
whiletruedobegin
sleep(20000);
if SecondsBetween(TimeNow, Now) >= Floor(4 * 60 * 60) thenbegin
DoNotification;
TimeNow := Now;
end;
end;
end);
Your notification looks more complicated than it needs to be, and the unnecessary loop you used there might be the cause of your segmentation fault.
myNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := 'ServiceNotification';
MyNotification.Title := 'Android Service Notification';
MyNotification.AlertBody := 'hello host, I'm your service'
MyNotification.FireDate := Now;
NotificationCenter1.PresentNotification(MyNotification);
finally
myNotification.DisposeOf;
end;
The good rule of thumb for Android services in Delphi is to write the simplest code you can to get the job done and no more.
Solution 2:
You don't need a service to do this, as 323go suggested.
a service should only be used when it is definitely needed because it consumes system resources and drains battery life, so use them on a need to only basis.
You can use the native AlarmManager.setRepeating
method that will create a native recurring notification in a custom interval specified by you, the solution however is Android specific
refer to my answer here Custom notification interval
Just note that for your case that MyNotification.RepeatIntervalinMills
is the interval in Milliseconds that you want your notifications to fire in so in your case of 4 Hours it would be
MyNotification.RepeatIntervalinMills := 14400000// That is 4 hours in Milliseconds
Post a Comment for "Delphi 10 Seattle Background Service And Threads"