我不知道我的代码中有什么问题!
它的工作但是当我关闭应用程序时,服务并不起作用!再次启动应用程序时计数器重新启动!任何帮助 我的代码:
主要:
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.
它的工作但是当我关闭应用程序时,服务并不起作用! 再次启动应用程序时计数器重新启动! 任何帮助
由于
答案 0 :(得分:2)
本地服务在与启动它的应用程序相同的过程中运行。因此,当应用程序终止时,服务也会终止。
如果您需要一个比其主机应用程序更长的长时间运行服务,请在主机应用程序中的工作线程而不是主线程中启动该服务,并调用服务中的Service.startForeground()
方法{ {1}}事件。
或者,您可能需要在其清单中将服务标记为远程服务而不是本地服务,因此服务在其自己的进程中运行。然后,您的主应用程序可以根据需要与服务进行通信,例如使用Intents。
答案 1 :(得分:1)
AlarmManager
program PlayerPowerMgr;
uses
AndroidApi.JNI.GraphicsContentViewText,
Androidapi.Helpers,
Androidapi.JNI.App,
Androidapi.NativeActivity,
Androidapi.AppGlue,
Androidapi.JNI.Embarcadero,
ServiceUnit in '..\PowerMgrService\ServiceUnit.pas' {DM: TAndroidService};
{$R *.res}
procedure StartService;
var
Intent: JIntent;
PendingIntent: JPendingIntent;
const
ServicePackageName = 'com.embarcadero.services.PlayerPwrMgrServ';
ApplicationPackageName = 'com.embarcadero.PlayerPowerMgr';
begin
Intent := TJIntent.Create;
Intent.setClassName(
StringToJString(ApplicationPackageName),
StringToJString(ServicePackageName)
);
PendingIntent := TJPendingIntent.JavaClass.getService(TAndroidHelper.Context, 1, Intent, 0);
TAndroidHelper.AlarmManager.&set(TJAlarmManager.JavaClass.RTC_WAKEUP, 1, PendingIntent);
end;
begin
StartService;
TJFMXNativeActivity.Wrap(PAndroid_app(PANativeActivity(System.DelphiActivity)^.instance)^.activity.clazz).finish;
end.
以下是启动服务并立即关闭的应用程序的代码。
ServiceUnit in '..\PowerMgrService\ServiceUnit.pas' {DM: TAndroidService};
当然,在项目经理的帮助下,您必须将服务添加到您的应用程序中。此行将出现
TJFMXNativeActivity.Wrap(PAndroid_app(PANativeActivity(System.DelphiActivity)^.instance)^.activity.clazz).finish;
关闭申请:
MainActivity.Finish
等同于
View Source
,但应用程序apk的大小将减少约10M。
创建这样的应用程序
1)创建一个应用程序作为多设备应用程序/空白应用程序
2)在项目经理中从应用程序中删除Unit1。
3)在项目经理中选择应用程序并单击" **You can write in model also using model object**
public function YourFunction(){
return "data";
}
**call it in your controller like this make model object suppose your
model name is YourModel in controller you can call like this**
$model = new YourModel();
$data = $model->YourFunction(); //calling method in controller
",然后插入此代码
4)将您的服务添加到应用程序