android - 带计时器的 embarcadero delphi XE10 android 服务

标签 android delphi service timer

我正在开发我的第一个服务,但遇到了麻烦。我使用来自 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('我是服务');

当我想使用计时器向服务器重复发送消息时,问题就出现了。所以我将一个计时器设置为“假”形式,计时器处于 Activity 状态,并且每 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.

在这种情况下,应用程序无法启动服务并且不响应。有什么帮助吗?非常感谢。

最佳答案

我发现 Seattle 10 的服务没有计时器。似乎没有 fmx 员工在服务上工作。

我使用的是带有 sleep(x) 的线程来模拟计时器。我为每个要重复的任务打开一个线程。例如:

procedure Thinfinito;
 var
  atask : Itask;


begin

atask := Ttask.create(procedure()
      var
      hacer: boolean;
      ahora : Tdatetime;
     begin
      hacer := false;
        REPEAT
          begin
            sleep(10000);
            ahora := now;
            v.IdUDPClient1.Send(TimeToStr(ahora)+' = soy el servicio');
          end;
        UNTIL hacer = true;;
     end);

 atask.Start;

end;

此线程每 10 秒从服务向服务器发送一条消息。没有尽头......好吧,直到服务被杀死。我正在努力。

关于android - 带计时器的 embarcadero delphi XE10 android 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34245482/

相关文章:

Android连接到127.168.1.1,怎么样?

delphi - 在 Delphi 中使用 Lambda 函数

c# - 为什么StreamWriter在Windows Service中不能工作?

wcf - 在 Azure 网站内托管 Azure 服务总线中继的 WCF 服务终结点

android - SkImageDecoder::Factory 返回空

java - 找不到符号 findViewById

java - Json解析异常: unable to find value in json array

delphi - 使用 Indy 发布并且文件名包含希腊字符时文件上传失败

delphi - SSL3_GET_RECORD :wrong version number

vb.net - System.ServiceProcess.ServiceController 未定义 vb.net