本文介绍了如何检测Android中的程序的终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows程序终止,它调用的事件处理程序一样的OnClose,和的OnDestroy析构函数销毁。当我想节省一些INI设置这些都是地方要。我写的所有这些事件的事件处理程序,但是当我终止程序,他们不被处理。

When a Windows program terminates it calls event handlers like OnClose, OnDestroy and the destructor Destroy. When I want to save some INI settings these are the places to be. I wrote event handlers for all these events but they are not processed when I terminate the program.

有谁知道我应该把code当一个Android程序终止要执行?我强烈怀疑这适用于iOS为好。

Does anyone know where I should place the code to be executed when an Android program terminates? I strongly suspect that this applies to iOS as well.

更新

约翰的回答适用于Android作为很好,尽管现实比他的例子稍微复杂一些。这种做法的好处是,它迫使我到TApplicationEvents,这是我从来没有听说过的。由于是定制不是由英巴卡迪诺记录,但FMX.Platform的code是足够有趣。几个ApplicationEvents是其中定义了三个似乎兴趣:aeEnteredBackground,aeWillBecomeInactive和aeWillTerminate。因为它们没有证明我presumed,他们做了什么,他们的名字建议:信令背景状态已经达到了,它西港岛线开始去的背景,它西港岛线(非常)即将终止。我适应约翰的code如下:

Johan's answer works for Android as well, although reality is slightly more complicated than his examples. The nice thing was that it forced me into TApplicationEvents, something I'd never heard of. As is custom not documented by Embarcadero but the code of FMX.Platform is interesting enough. Several ApplicationEvents are defined of which three seem of interest: aeEnteredBackground, aeWillBecomeInactive and aeWillTerminate. As they are not documented I presumed that they did what their names suggested: signalling that a background state has been reached, that it wil start to go to background and that it wil (very) soon terminate. I adapted Johan's code as follows:

  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。如果你的code应该被执行一次,然后把你的precautions。不过还好的是:aeWillTerminate做什么它advertizes:当应用程序被终止它发出了一个信号。时间这样做很可能是短暂的,我会测试它是否足以写一TIniFile。

When I label the events 1, 2 and 3 experiments with the debugger showed the following: forcing the application to the background generates a sequences of events: 2, 1, 1, 2. Once I even got 2, 2, 1, 1, 2, 2. If your code should be executed once, then take your precautions. But Better is: the aeWillTerminate does what it advertizes: it sends a signal when the application is terminated. Time to do so is likely to be brief and I will test whether it suffices to write a TIniFile.

我试过在Win32中这个code,以及那是行不通的。该AppEvent不会被解雇。这迫使我立即测试code在我的平板电脑这需要一些时间。可惜了。

I tried this code in Win32 as well and that does not work. The AppEvent is not fired. That forces me to test the code immediately on my tablet which takes some time. Pity.

推荐答案

在iOS应用程序很少接近,但进入后台模式。结果
这就是为什么OnClose事件不火。我猜想,通过点击在任务管理器的X杀一个应用程序实际上是强行终止应用程序,但还没有测试过这一点。的在任何情况下,这种使用情况太罕见打击code。的结果
在Android中工作的事情pretty大同小异。

In iOS applications seldom close but enter a background mode.
This is why the OnClose event does not fire. I suspect that killing an app by clicking on the 'x' in the taskmanager actually forcefully terminates the app, but haven't tested this. in any case this use case is too rare to code against.
In Android things work pretty much the same.

幸运的是安德斯·奥尔森写了一个非常丰富的博客文章有关此主题,在这里看到:的。结果
下面建立后上赶上实际的一个后台

Luckily Anders Ohlsson has written a very informative blog post about this subject, see here: http://blogs.embarcadero.com/ao/2013/05/01/39450.
The following post builds on that to catch the actual backgrounding https://forums.embarcadero.com/message.jspa?messageID=558241

关键是要申请注册事件。请参阅:

The trick is to register for application events. See: http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Platform.TApplicationEvent

一些示例code适用于iOS不具备的Andr​​oid得心应手,对不起。结果
从上面的论坛复制:

Some sample code for iOS don't have Android handy, sorry.
Copy from the above forum:

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明智的事件处理程序,但他们没有。 结果
也许你应该扩展TApplication的添加这些事件处理器,这样的理智可以preserved。结果
我建议您提交QA报告。

Obviously these events should have triggered sensible event handlers in FMX.Platform.TApplication but they don't. http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Forms.TApplication_Events
Perhaps you should extend TApplication to add these eventhandlers so that sanity can be preserved.
I recommend filing a QA report.

下面是扩展TApplication的类的建议。

Here's a suggestion for the extended TApplication class.

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中的程序的终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:56