embarcadero delphi XE10 android service with a tim

2019-06-05 01:50发布

问题:

I am developing my first service and I am having troubles. I am using as basis this example from embarcadero:

http://docwiki.embarcadero.com/CodeExamples/Seattle/en/FMX.Android_Notification_Service_Sample

When the service starts I send a message to a server and it works fine. I only need to drop the UDPclient on the form and I add one line of code. This is the code and it works:

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.

So I only added IdUDPClient1.Send('I am a service');

The problem arises when I want to use a timer to send repetitively a message to the server. So I drop a timer to the "false" form, timer is active, and each 5 seconds sends a message to the server.

The new code is:

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.

In this case, the Application cannot start the service and it does not respond. Any help? Thanks a lot in advance.

回答1:

I found timers do not work on services on Seattle 10. and it seems no fmx staff work on services.

What I am using are threads with sleep(x) to simulate a timer. I open a thread for each task to be repeated. For example:

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;

This thread sends a message to a server from the service each 10 seconds. No end...well until the service is killed. I am working on that.