I am developing my first service and I am having troubles. I am using as basis this example from embarcadero:
http://docwiki.embarcadero.com/CodeExamples/Seattle/en/FMX.Android_Notification_Service_Sample
When the service starts I send a message to a server and it works fine. I only need to drop the UDPclient on the form and I add one line of code. This is the code and it works:
unit NotificationServiceUnit;
interface
uses
System.SysUtils,
System.Classes,
System.Android.Service,
AndroidApi.JNI.GraphicsContentViewText,
Androidapi.JNI.Os, System.Notification, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPClient;
type
TNotificationServiceDM = class(TAndroidService)
NotificationCenter1: TNotificationCenter;
IdUDPClient1: TIdUDPClient;
function AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
private
{ Private declarations }
FThread: TThread;
procedure LaunchNotification;
public
{ Public declarations }
end;
var
NotificationServiceDM: TNotificationServiceDM;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
uses
Androidapi.JNI.App, System.DateUtils;
{$R *.dfm}
function TNotificationServiceDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags,
StartId: Integer): Integer;
begin
IdUDPClient1.Send('I am a service');
LaunchNotification;
JavaService.stopSelf;
Result := TJService.JavaClass.START_STICKY;
end;
procedure TNotificationServiceDM.LaunchNotification;
var
MyNotification: TNotification;
begin
MyNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := 'ServiceNotification';
MyNotification.Title := 'Android Service Notification';
MyNotification.AlertBody := 'RAD Studio 10 Seattle';
MyNotification.FireDate := IncSecond(Now, 8);
NotificationCenter1.ScheduleNotification(MyNotification);
finally
MyNotification.Free;
end;
end;
end.
So I only added IdUDPClient1.Send('I am a service');
The problem arises when I want to use a timer to send repetitively a message to the server. So I drop a timer to the "false" form, timer is active, and each 5 seconds sends a message to the server.
The new code is:
unit NotificationServiceUnit;
interface
uses
System.SysUtils,
System.Classes,
System.Android.Service,
AndroidApi.JNI.GraphicsContentViewText,
Androidapi.JNI.Os, System.Notification, IdBaseComponent, IdComponent,
IdUDPBase, IdUDPClient, FMX.Types;
type
TNotificationServiceDM = class(TAndroidService)
NotificationCenter1: TNotificationCenter;
IdUDPClient1: TIdUDPClient;
Timer1: TTimer;
function AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
private
{ Private declarations }
FThread: TThread;
procedure LaunchNotification;
public
{ Public declarations }
procedure Timer1Timer(Sender: TObject);
end;
var
NotificationServiceDM: TNotificationServiceDM;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
uses
Androidapi.JNI.App, System.DateUtils;
{$R *.dfm}
function TNotificationServiceDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags,
StartId: Integer): Integer;
begin
IdUDPClient1.Send('I am a service');
LaunchNotification;
JavaService.stopSelf;
Result := TJService.JavaClass.START_STICKY;
end;
procedure TNotificationServiceDM.LaunchNotification;
var
MyNotification: TNotification;
begin
MyNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := 'ServiceNotification';
MyNotification.Title := 'Android Service Notification';
MyNotification.AlertBody := 'RAD Studio 10 Seattle';
MyNotification.FireDate := IncSecond(Now, 8);
NotificationCenter1.ScheduleNotification(MyNotification);
finally
MyNotification.Free;
end;
end;
procedure TNotificationServiceDM.Timer1Timer(Sender: TObject);
begin
IdUDPClient1.Send('I send from the timer');
end;
end.
In this case, the Application cannot start the service and it does not respond. Any help? Thanks a lot in advance.