【问题标题】:embarcadero delphi XE10 android service with a timerembarcadero delphi XE10 带有计时器的android服务
【发布时间】: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


【解决方案1】:

我发现在西雅图 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 秒从服务向服务器发送一条消息。没有尽头......好吧,直到服务被杀死。我正在努力。

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2017-11-08
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 2013-04-20
    相关资源
    最近更新 更多