我正在開發我的第一個服務,我有麻煩。我使用作爲基礎這個例子從英巴卡迪諾:embarcadero delphi XE10 android服務與定時器
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