【问题标题】:How to detect the termination of a program in Android?如何检测Android中程序的终止?
【发布时间】:2013-10-28 03:43:37
【问题描述】:

当 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 不会被触发。这迫使我立即在平板电脑上测试代码,这需要一些时间。可惜。

【问题讨论】:

    标签: android delphi firemonkey delphi-xe5


    【解决方案1】:

    在 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. 
    

    【讨论】:

    • 感谢您的详细回答并 +1!我必须为Android测试它。当我有更多要报告的时候,我会回来的。我忘了提到我也尝试过 OnDeactivate 无济于事。您的解决方案可以很好地替代 OnDeactivate。
    【解决方案2】:

    当用户离开您的活动(屏幕)时,系统会调用 onStop() 来停止活动。如果用户在活动停止时返回,系统会调用 onRestart(),紧接着是 onStart() 和 onResume()。因此,建议编写代码并将其放在 onStop() 方法的主体中。

    http://developer.android.com/images/training/basics/basic-lifecycle-stopped.png

    【讨论】:

    • 请不要在您对该主题一无所知的地方发布答案。如果您不熟悉 delphi 和 delphi xe5,则不应回答带有这些标签的问题。
    猜你喜欢
    • 1970-01-01
    • 2010-11-25
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多