【发布时间】:2016-11-19 11:51:00
【问题描述】:
我不知道我的代码有什么问题!
它可以工作,但是当我关闭应用程序时服务不起作用!!!再次启动应用程序时计数器会重新启动!!!任何帮助 我的代码:
主要:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.IOUtils,
System.Android.Service, FMX.ScrollBox, FMX.Memo, FMX.Controls.Presentation,
FMX.StdCtrls
;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FService : TLocalServiceConnection;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
begin
FService := TLocalServiceConnection.Create;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FService.StartService('AndroidServer');
end;
end.
服务:
unit Unit2;
interface
uses
System.SysUtils, System.Threading,
System.Classes, System.IOUtils,
System.Android.Service,
AndroidApi.JNI.GraphicsContentViewText,
Androidapi.JNI.Os, System.Notification;
type
TAndroidServiceDM = class(TAndroidService)
NotificationCenter1: TNotificationCenter;
function AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
procedure AndroidServiceCreate(Sender: TObject);
Procedure DoNotification;
procedure AndroidServiceTaskRemoved(const Sender: TObject;
const ARootIntent: JIntent);
procedure AndroidServiceDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
AndroidServiceDM: TAndroidServiceDM;
io:Integer;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
uses
Androidapi.JNI.App;
procedure TAndroidServiceDM.DoNotification;
Var
MyNotification: TNotification;
begin
TTask.Run(procedure
begin
myNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := 'ServiceNotification';
MyNotification.Title := 'Android Service Notification';
MyNotification.AlertBody := io.ToString;
MyNotification.FireDate := Now;
NotificationCenter1.PresentNotification(MyNotification);
finally
myNotification.DisposeOf;
end;
tthread.Synchronize(nil,
procedure
begin
end);
end
) ;
end;
procedure TAndroidServiceDM.AndroidServiceCreate(Sender: TObject);
var
atask : Itask;
begin
io:=1;
atask := Ttask.create(procedure()
begin
while true do
begin
begin
sleep(5000);
DoNotification;
io:=io+1;
end;
end;
end);
atask.Start;
end;
procedure TAndroidServiceDM.AndroidServiceDestroy(Sender: TObject);
begin
end;
function TAndroidServiceDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Result := TJService.JavaClass.START_STICKY;
end;
end.
它可以工作,但是当我关闭应用程序时服务不起作用!!! 再次启动应用程序时计数器会重新启动!!! 任何帮助
谢谢
【问题讨论】:
-
没有XE10这样的Delphi版本。有 Delphi 10 Seattle 和 Delphi 10.1 Berlin,两者都不是 XE10。
-
德尔福 10 西雅图和德尔福 10.1 柏林