android终止程序,如何检测Android中程序的终止?

Windows程序终止时,它将调用事件处理程序,例如OnClose,OnDestroy和析构函数Destroy.当我要保存一些INI设置时,这些地方就是.我为所有这些事件编写了事件处理程序,但在终止程序时未对其进行处理.

有谁知道我应该在Android程序终止时将要放置的代码放在哪里?我强烈怀疑这也适用于iOS.

更新资料

Johan的答案也适用于Android,尽管现实情况比他的示例要复杂得多.令人高兴的是,它迫使我进入了TApplicationEvents,这是我从未听说过的.正如Embarcadero没有记录的自定义文件一样,但是FMX.Platform的代码足够有趣.定义了几个ApplicationEvent,其中三个似乎很有趣:aeEnteredBackground,aeWillBecomeInactive和aeWillTerminate.由于未记录这些文件,因此我假设它们按照其名称的建议进行了操作:表示已达到背景状态,该状态将开始进入背景状态,并且(非常)将很快终止.我修改了Johan的代码,如下所示:

function TForm2.AppEvent (AAppEvent: TApplicationEvent; AContext: TObject) : Boolean;

begin

// do something here for when the app is sent to background

case AAppEvent of

(1) TApplicationEvent.aeEnteredBackground: ;// Something for OnDeactivated

// which does not exist

(2) TApplicationEvent.aeWillBecomeInactive: if Assigned (OnDeactivate)

then OnDeactivate (Self);

(3) TApplicationEvent.aeWillTerminate: if Assigned (OnClose)

then OnClose (Self);

end; // case

Result := True; // let iOS/Android know it worked...

end; // AppEvent //

当我标记事件1、2和3时,调试器的实验显示如下:将应用程序强制到后台会生成一系列事件:2、1、1、2.一旦我得到了2、2、1、1, 2,2.如果您的代码应执行一次,请多加注意.但更好的是:aeWillTerminate做了广告:在应用程序终止时发送信号.这样做的时间可能很短暂,我将测试编写TIniFile是否足够.

我也在Win32中尝试了此代码,但无法正常工作.未触发AppEvent.这迫使我立即在平板电脑上测试代码,这需要一些时间.可怜.

解决方法:

在iOS应用程序中,很少关闭但进入后台模式.

这就是为什么OnClose事件不会触发的原因.我怀疑通过单击任务管理器中的“ x”杀死应用程序实际上会强制终止该应用程序,但尚未对此进行测试.在任何情况下,这种用例都很少发生,无法进行编码.

在Android中,工作原理几乎相同.

抱歉,iOS的一些示例代码没有Android.

从以上论坛复制:

unit Unit1;

interface

uses

System.SysUtils, System.Classes, FMX.Forms, FMX.Platform;

type

TForm1 = class(TForm)

procedure FormCreate(Sender: TObject);

private

public

function AppEvent(AAppEvent: TApplicationEvent; AContext: TObject) : Boolean;

end;

var

Form1: TForm1;

implementation

{$R *.fmx}

function TForm1.AppEvent(AAppEvent: TApplicationEvent; AContext: TObject) : Boolean;

begin

if AAppEvent = TApplicationEvent.aeEnteredBackground then begin

// do something here for when the app is sent to background

end;

Result := True; // let iOS know it worked...

end;

procedure TForm1.FormCreate(Sender: TObject);

var

AppEventSvc: IFMXApplicationEventService;

begin

if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(AppEventSvc)) then

AppEventSvc.SetApplicationEventHandler(AppEvent);

end;

end.

显然,这些事件应该已经触发了FMX.Platform.TApplication中的明智事件处理程序,但事实并非如此. http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Forms.TApplication_Events

也许您应该扩展TApplication来添加这些事件处理程序,以便可以保持理智.

我建议提交质量检查报告.

这是扩展的TApplication类的建议.

type

TnotifyApplication = class(FMX.Platfrom.TApplication)

private

FOnStop: TnotifyEvent;

protected

procedure AppEvent(AAppEvent: TApplicationEvent; AContext: TObject): boolean;

procedure SetOnStop(value: TNotifyEvent);

procedure DoOnStop;

public

property OnStop: TNotifyEvent read FOnStop write SetOnStop;

end;

implementation

procedure TNotifyApplication.SetOnStop(value: TNotifyEvent);

begin

if Assigned(value) then begin

//register for the notification to call AppEvent

end else begin

//

end;

end;

procedure TNotifyApplication.DoOnStop;

begin

if Assigned(FOnStop) then FOnStop(self);

end;

procedure TNotifyApplication.AppEvent(AAppEvent: TApplicationEvent; AContext: TObject) : Boolean;

begin

//call the relevant do... Call depending in the exact event.

标签:firemonkey,delphi-xe5,android,delphi

来源: https://codeday.me/bug/20191122/2060889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值