I write an app in delphi! For this app have services. In Services is set alarm, that a function repeating! When down run the alarm second the app will closed! What problem in the my services code? Help me please!
Update: I use delphi 11 and android 64bit.
this is my services:
function SecondsUntilMidnight: Int64;
begin
Result := SecondsBetween(Now, Trunc(Now + 1));
end;
function GetTimeFromNowInMillis(const ASeconds: Integer): Int64;
var
LCalendar: JCalendar;
begin
LCalendar := TJCalendar.JavaClass.getInstance;
if ASeconds > 0 then
LCalendar.add(TJCalendar.JavaClass.SECOND, ASeconds);
Result := LCalendar.getTimeInMillis;
end;
function GetMidnightInMillis: Int64;
begin
Result := GetTimeFromNowInMillis(SecondsUntilMidnight);
end;
procedure TServiceModule.CreateAlarmIntent(const AAction: string);
var
LActionIntent: JIntent;
begin
LActionIntent := TJIntent.JavaClass.init(StringToJString(AAction));
LActionIntent.setClassName(TAndroidHelper.Context.getPackageName, StringToJString(cReceiverName));
LActionIntent.putExtra(StringToJString('ServiceName'), StringToJString(cServiceName));
FAlarmIntent := TJPendingIntent.JavaClass.getBroadcast(TAndroidHelper.Context, 0, LActionIntent, TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT);
end;
procedure TServiceModule.SetAlarm(const AAction: string; AStartAt: Int64 = 0; AInterval: Int64 = 0);
begin
if AStartAt = 0 then
AStartAt := GetTimeFromNowInMillis(0);
StopAlarm;
FInterval := AInterval;
CreateAlarmIntent(AAction);
if FInterval > 0 then
begin
// Allow for alarms while in "doze" mode
if TOSVersion.Check(6) then
TAndroidHelper.AlarmManager.setExactAndAllowWhileIdle(TJAlarmManager.JavaClass.RTC_WAKEUP, GetTimeFromNowInMillis(AInterval), FAlarmIntent)
else
TAndroidHelper.AlarmManager.setRepeating(TJAlarmManager.JavaClass.RTC_WAKEUP, AStartAt, AInterval, FAlarmIntent);
end
else
TAndroidHelper.AlarmManager.&set(TJAlarmManager.JavaClass.RTC_WAKEUP, AStartAt, FAlarmIntent);
end;
procedure TServiceModule.SetDailyAlarm(const AAction: string; const AStartAt: Int64);
begin
SetAlarm(AAction, AStartAt, TJAlarmManager.JavaClass.INTERVAL_DAY);
end;
procedure TServiceModule.StopAlarm;
begin
if FAlarmIntent <> nil then
TAndroidHelper.AlarmManager.cancel(FAlarmIntent);
FAlarmIntent := nil;
end;
procedure TServiceModule.AndroidServiceDestroy(Sender: TObject);
begin
Stopalarm;
end;
function TServiceModule.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
var
Notification: TNotification;
begin
TOSLog.d('Service started');
if (Intent <> nil) and JStringToString(Intent.getAction).Equals(cActionServiceAlarm) then
begin
TOSLog.d('Alarm was triggered');
// Do whatever should happen as a result of the alarm
Notification := NotificationC.CreateNotification;
try
Notification.AlertBody := 'Notification Test is ok';
Notification.FireDate := Now;
{ Send notification in Notification Center }
NotificationC.PresentNotification(Notification);
//table.Close;
finally
Notification.Free;
end;
SetAlarm(cActionServiceAlarm, GetTimeFromNowInMillis(20));
// Reset the alarm if on Android 6 or greater, to allow for alarms while in "doze" mode
if TOSVersion.Check(6) and (FInterval > 0) then
SetAlarm(cActionServiceAlarm, 0, FInterval);
end
// Set an alarm - a result of True means one was set (i.e. one had not been set before), so you might want to take some action in that case
// if SetDailyAlarm(cActionServiceAlarm, MillisecondsUntilMidnight) then // <----- Daily at midnight example
else
SetAlarm(cActionServiceAlarm, GetTimeFromNowInMillis(20)); // <----- One off, in 1 minutes time
Result := TJService.JavaClass.START_STICKY;
end;