我不知道我的代碼中存在什麼問題!當我關閉應用程序時,Android服務不起作用
這是工作,但服務不起作用,當我關閉應用程序!並重新啓動應用程序時重新啓動計數器!任何幫助 我的代碼:
主營:
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。德爾福10西雅圖和德爾福10.1德國柏林,都不是XE10。 –
Delphi 10西雅圖和德爾福10.1柏林 –