具有定时Embarcadero的德尔福XE10的Andr​​oid服务 [英] embarcadero delphi XE10 android service with a timer

查看:925
本文介绍了具有定时Embarcadero的德尔福XE10的Andr​​oid服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展我的第一个服务,我有烦恼。我使用作为基础这个例子从Embarcadero的:

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

<一个href=\"http://docwiki.embarcadero.com/$c$cExamples/Seattle/en/FMX.Android_Notification_Service_Sample\" rel=\"nofollow\">http://docwiki.embarcadero.com/$c$cExamples/Seattle/en/FMX.Android_Notification_Service_Sample

在服务启动时将消息发送到服务器,它工作正常。我只需要删除UDPclient的形式和我加code的一条线。这是code和它的工作原理:

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.

所以,我只加了 IdUDPClient1.Send(我是一个服务');

当我想用一个定时器来重复信息发送到服务器的问题出现了。所以我滴计时器的假的形式,计时器是活动的,并分别5秒期间将消息发送到服务器。

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.

新的code是:

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.

推荐答案

我发现定时器不上的服务西雅图10工作,它似乎对服务不作任何FMX人员的工作。

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

我现在用的是睡眠线程(x)的模拟定时器。我打开可以重复每个任务的线程。例如:

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;

此线程从服务将消息发送到一个服务器的每个10秒。没有尽头......好吧,直到服务被杀害。我的工作这一点。

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.

这篇关于具有定时Embarcadero的德尔福XE10的Andr​​oid服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆