Android Service
Opened this issue · 0 comments
cayque10 commented
Hey guys.
I tested with Android Service, but it didn't work correctly. When the FTimerQueue.Add method is executed, the application crashes.
Is it possible to use the timer in an Android service?
Thanks
Used code to service
unit NotificationServiceUnit;
interface
uses
System.SysUtils,
System.Classes,
System.Android.Service,
AndroidApi.JNI.GraphicsContentViewText,
AndroidApi.JNI.Os,
System.Notification,
Grijjy.TimerQueue;
type
TNotificationServiceDM = class(TAndroidService)
NotificationCenter1: TNotificationCenter;
function AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
procedure AndroidServiceCreate(Sender: TObject);
procedure AndroidServiceDestroy(Sender: TObject);
private
{ Private declarations }
FThread: TThread;
FTimerQueue: TgoTimerQueue;
FTimerHandle: THandle;
procedure LaunchNotification(const pMessage: UTF8String);
procedure OnTimer(const ASender: TObject);
public
{ Public declarations }
end;
var
NotificationServiceDM: TNotificationServiceDM;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
uses
AndroidApi.JNI.App;
{$R *.dfm}
procedure TNotificationServiceDM.AndroidServiceCreate(Sender: TObject);
begin
FTimerQueue := TgoTimerQueue.Create;
end;
procedure TNotificationServiceDM.AndroidServiceDestroy(Sender: TObject);
begin
FTimerQueue.Release(FTimerHandle);
FTimerQueue.Free;
end;
function TNotificationServiceDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent;
Flags, StartId: Integer): Integer;
begin
FTimerHandle := FTimerQueue.Add(10000, OnTimer);
JavaService.stopSelf;
Result := TJService.JavaClass.START_STICKY;
end;
procedure TNotificationServiceDM.LaunchNotification(const pMessage: UTF8String);
var
MyNotification: TNotification;
begin
MyNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := 'ServiceNotification';
MyNotification.Title := 'Android Service Notification';
MyNotification.AlertBody := String(pMessage);
NotificationCenter1.PresentNotification(MyNotification);
finally
MyNotification.Free;
end;
end;
procedure TNotificationServiceDM.OnTimer(const ASender: TObject);
begin
TThread.Synchronize(TThread.Current,
procedure
begin
LaunchNotification('test');
end);
end;
end.