embarcadero delphi XE10安卓服务与计时器

时间:2015-12-12 21:59:02

标签: 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('我是服务');

当我想使用计时器重复向服务器发送消息时,会出现问题。所以我将一个计时器丢弃到“假”形式,计时器处于活动状态,每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.

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

1 个答案:

答案 0 :(得分:2)

我发现计时器不适用于西雅图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秒从服务向服务器发送一条消息。没有结束......直到服务被杀。我正在努力。