Я занимаюсь разработкой своего первого сервиса, и у меня возникают проблемы. Я использую в качестве основы этого пример из Embarcadero:embarcadero delphi XE10 android service с таймером
http://docwiki.embarcadero.com/CodeExamples/Seattle/en/FMX.Android_Notification_Service_Sample
Когда служба запускается я отправить сообщение на сервер и он работает отлично. Мне нужно только удалить UDPclient в форме и добавить одну строку кода. Это код и это работает:
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.
Так что я только добавил IdUDPClient1.Send ('Я услуга');
Проблема возникает, когда я хочу использовать таймер для повторного отправки сообщения на сервер. Поэтому я бросаю таймер в «ложную» форму, таймер активен, и каждые 5 секунд отправляет сообщение серверу.
Новый код:
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.
В этом случае приложение не может запустить службу, и он не отвечает. Любая помощь? Заранее большое спасибо.
Я удалил JavaService.stopSelf; без улучшения – Francisco