2015-12-12 62 views
0

我正在開發我的第一個服務,我有麻煩。我使用作爲基礎這個例子從英巴卡迪諾:embarcadero delphi XE10 android服務與定時器

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. 

在這種情況下,應用程序無法啓動該服務,並沒有作出迴應。任何幫助?提前致謝。

+0

我已經刪除了JavaService.stopSelf;沒有改進 – Francisco

回答

1

我發現定時器不能在西雅圖10上的服務上工作,而且似乎沒有fmx員工在服務上工作。

我正在使用的是帶睡眠(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秒。沒有結束......直到服務被殺。我正在爲此工作。

+0

這是否在應用程序關閉時工作? – nurettin