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

标签 android delphi firemonkey delphi-xe5

当 Windows 程序终止时,它会调用事件处理程序,如 OnClose、OnDestroy 和析构函数 Destroy。当我想保存一些 INI 设置时,这些就是我要去的地方。我为所有这些事件编写了事件处理程序,但当我终止程序时它们没有得到处理。

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

更新

Johan 的回答也适用于 Android,尽管实际情况比他的示例稍微复杂一些。好消息是它迫使我进入 TApplicationEvents,这是我从未听说过的东西。正如 Embarcadero 没有记录的那样,但是 FMX.Platform 的代码非常有趣。定义了几个 ApplicationEvents,其中三个看起来很有趣: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 中,事情几乎是一样的。

幸运的是,Anders Ohlsson 写了一篇关于这个主题的非常有用的博客文章,请参见此处:http://blogs.embarcadero.com/ao/2013/05/01/39450 .
以下帖子以此为基础来捕捉实际背景 https://forums.embarcadero.com/message.jspa?messageID=558241

诀窍是注册应用程序事件。请参阅:http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Platform.TApplicationEvent

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. 

关于android - 如何检测Android中程序的终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19626892/

相关文章:

android - 在哪里以及如何找到 Android 类使用的布局文件?

java - 将.class文件转换为smali文件并重新打包.apk

java - 新建的 SQLitedatabase 表不存在

macos - FIREMONKEY - 运行启动器来启动应用程序 (OSX)

java - Maven android 编译多个源码目录

delphi - 带 Rad Studio 10.2 的 Nox 模拟器

delphi - 如何捕获自定义变体的数组索引

delphi - Delphi中的圆角矩形

delphi - Firemonkey TFrame 对齐 = 缩放错误?

delphi - 为自定义组件创建样式的正确方法