【发布时间】:2015-12-12 21:59:02
【问题描述】:
我正在开发我的第一个服务,但遇到了麻烦。我使用来自 embarcadero 的这个例子作为基础:
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('I am a service');
当我想使用计时器重复向服务器发送消息时,就会出现问题。所以我把一个计时器放到“假”表单上,计时器处于活动状态,每 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;没有改善
标签: android delphi service timer