Http://blog.sina.com.cn/s/blog_44fa172f0102vwr2.html
With the code directly, there is also the experience that Setapplicationeventhandler can register multiple event methods.
Unit Unit6;
Interface
Uses
System.sysutils, System.types, System.uitypes, system.classes, System.variants,
FMX. Types, FMX. Controls, FMX. Forms, FMX. Graphics, FMX. Dialogs,
FMX. Controls.presentation, FMX. Scrollbox, FMX. Memo, FMX. Platform;
Type
TFORM6 = Class (Tform)
Memo1:tmemo;
Procedure Formcreate (Sender:tobject);
Private
function Handleappevent (aappevent:tapplicationevent;
Acontext:tobject): Boolean;
{Private declarations}
Public
{Public declarations}
End
Var
FORM6:TFORM6;
Implementation
{$R *.FMX}
Procedure Tform6.formcreate (Sender:tobject);
Var
Svcevents:ifmxapplicationeventservice;
Begin
If TPlatformServices.Current.SupportsPlatformService
(Ifmxapplicationeventservice, IInterface (svcevents))
Then
Svcevents.setapplicationeventhandler (handleappevent);
End
function Tform6.handleappevent (aappevent:tapplicationevent; Acontext:tobject): Boolean;
Var
astate:string;
Begin
Case Aappevent of
Tapplicationevent.finishedlaunching:
Astate: = ' finishedlaunching ';
Tapplicationevent.becameactive:
Astate: = ' becameactive ';
Tapplicationevent.willbecomeinactive:
Astate: = ' willbecomeinactive ';
Tapplicationevent.enteredbackground:
Astate: = ' enteredbackground ';
Tapplicationevent.willbecomeforeground:
Astate: = ' willbecomeforeground ';
Tapplicationevent.willterminate:
Astate: = ' willterminate ';
Tapplicationevent.lowmemory:
Astate: = ' lowmemory ';
Tapplicationevent.timechange:
Astate: = ' timechange ';
Tapplicationevent.openurl:
Astate: = ' OpenURL ';
End
SELF.MEMO1.LINES.ADD (astate);
Result: = true;
End
End.
You can also refer to the following example, which is the type of message
FMX. Platform.tapplicationeventFMX. Platform.tapplicationevent
Http://docwiki.embarcadero.com/Libraries/Seattle/en/FMX.Platform.TApplicationEvent
Http://docwiki.embarcadero.com/Libraries/Tokyo/en/FMX.Platform.TApplicationEvent
(finishedlaunching, becameactive, willbecomeinactive, Enteredbackground, Willbecomeforeground, Willterminate,lowmemory, Timechange, OpenURL);
An instance of Tapplicationevent is any of the following values:
Item |
Description |
Platform |
Android |
IOS |
BecameActive
|
Your application has gained the focus.//applicationdidbecomeactive |
Supported |
Supported |
EnteredBackground
|
The user is no longer using your application and your application is still running in the background. |
Supported |
Supported |
FinishedLaunching
|
Your application has been launched. |
Supported |
Supported |
lowmemory |
This warns your application so the device is running O UT of memory. Your application should reduce memory usage, freeing structures and data that can is loaded again at a later point. |
Supported |
Supported |
OpenURL |
You application have received a request to open an URL. Application Events of this type is usually associated with a context. This context was an instance of the ios-only tiosopenapplicationcontext class, which provides the F Ollowing read-only Properties:
-
tiosopenapplicationcontext.sourceapp is a string that C Ontains The bundle id of The application that requested your application to open the URL.
-
tiosopenapplicationcontext.url is The URL to open, either a network resource or a file.
-
tiosopenapplicationcontext.context is A pointer to A property-list Object that might Provide additional information.
See The ios APIs reference documentation for more information. |
|
Supported |
TimeChange
|
There has been a significant. This event might happen for example when the day changes or when the device is changes to or from daylight savings time. |
|
Supported |
WillBecomeForeground
|
The user is now using your application, which were previously in the background. |
Supported |
Supported |
WillBecomeInactive
|
Your application is going to loose the focus. /applicationwillresignactive |
Supported |
Supported |
WillTerminate
|
The user is quitting your application. |
Supported |
Supported |
http://codeverge.com/embarcadero.delphi.ios/ ifmxapplicationeventservice-not-firing/2028062
http://codeverge.com/embarcadero.delphi.firemonkey/ keep-application-on-top/2009583
Procedure Tmainform.formcreate (Sender:tobject); var Svcevents:ifmxapplicationeventservice; Begin if TPlatformServices.Current.SupportsPlatformService (Ifmxapplicationeventservice, IInterface (svcevents)) Then Svcevents.setapplicationeventhandler (handleappevent); Application.onexception: = Exceptionhandler; End function Tmainform.handleappevent (aappevent:tapplicationevent; Acontext:tobject): Boolean; Begin case Aappevent of tapplicationevent.finishedlaunching:; Tapplicationevent.becameactive:; Tapplicationevent.willbecomeinactive:; Tapplicationevent.enteredbackground:; Tapplicationevent.willbecomeforeground:; Tapplicationevent.willterminate:; Tapplicationevent.lowmemory:; Tapplicationevent.timechange:; Tapplicationevent.openurl:; End Result: = True; End -See more at:http://codeverge.com/embarcadero.delphi.firemonkey/keep-application-on-top/2009583# Sthash.6xm5jeui.dpufprocedure tmainform.formcreate (Sender:tobject); var Svcevents:ifmxapplicationeventservice; Begin IF TPlAtformServices.Current.SupportsPlatformService (Ifmxapplicationeventservice, IInterface (svcevents)) Then Svcevents.setapplicationeventhandler (handleappevent); Application.onexception: = Exceptionhandler; End function Tmainform.handleappevent (aappevent:tapplicationevent; Acontext:tobject): Boolean; Begin case Aappevent of tapplicationevent.finishedlaunching:; Tapplicationevent.becameactive:; Tapplicationevent.willbecomeinactive:; Tapplicationevent.enteredbackground:; Tapplicationevent.willbecomeforeground:; Tapplicationevent.willterminate:; Tapplicationevent.lowmemory:; Tapplicationevent.timechange:; Tapplicationevent.openurl:; End Result: = True; End -See more At:http://codeverge.com/embarcadero.delphi.firemonkey/keep-application-on-top/2009583#sthash.6xm5jeui.dpuf
Procedure Tmainform.formcreate (Sender:tobject); var Svcevents:ifmxapplicationeventservice; Begin if TPlatformServices.Current.SupportsPlatformService (Ifmxapplicationeventservice, IInterface (svcevents)) Then Svcevents.setapplicationeventhandler (handleappevent); Application.onexception: = Exceptionhandler; End function Tmainform.handleappevent (aappevent:tapplicationevent; Acontext:tobject): Boolean; Begin case Aappevent of tapplicationevent.finishedlaunching:; Tapplicationevent.becameactive:; Tapplicationevent.willbecomeinactive:; Tapplicationevent.enteredbackground:; Tapplicationevent.willbecomeforeground:; Tapplicationevent.willterminate:; Tapplicationevent.lowmemory:; Tapplicationevent.timechange:; Tapplicationevent.openurl:; End Result: = True; End -See more At:http://codeverge.com/embarcadero.delphi.firemonkey/keep-application-on-top/2009583#sthash.6xm5jeui.dpuf
http://community.embarcadero.com/index.php/blogs/entry/mobile-app-lifecycle-events-handling-in-delphi-xe5-40067
http://codeverge.com/embarcadero.delphi.firemonkey/keep-application-on-top/2009583
FMX. Platform.pas
Procedure Tmainform.formcreate (Sender:tobject); var svcevents:ifmxapplicationeventservice;begin if TPlatformServices.Current.SupportsPlatformService (ifmxap Plicationeventservice, IInterface (svcevents)) then Svcevents.setapplicationeventhandler (HandleAppEvent); Application.onexception: = Exceptionhandler;end;function tmainform.handleappevent (aappevent:tapplicationevent; Acontext:tobject): Boolean;begin case Aappevent of tapplicationevent.finishedlaunching:; Tapplicationevent.becameactive:;//First run app Trigger, switch from background to also trigger tapplicationevent.willbecomeinactive:; Tapplicationevent.enteredbackground:;//Switch to Background tapplicationevent.willbecomeforeground:;//switch from background to foreground Tapplicationevent.willterminate:; Tapplicationevent.lowmemory:; Tapplicationevent.timechange:; Tapplicationevent.openurl:; End Result: = True;end;//see more At:http://codeverge.com/embarcadero.delphi.firemonkey/keep-application-on-top/2009583
vs. iOS
Two consecutive Hits home button
Click on the springboard in the taskbar or press the Home button, click the Home button one time
-(void) Applicationwillresignactive: (uiapplication *) application
Program back
The program gets the focus and returns to app-(void) applicationdidbecomeactive in the taskbar: (uiapplication *) application
Procedure Tmainform.formcreate (Sender:tobject); var Svcevents:ifmxapplicationeventservice; Begin if TPlatformServices.Current.SupportsPlatformService (Ifmxapplicationeventservice, IInterface (svcevents)) Then Svcevents.setapplicationeventhandler (handleappevent); Application.onexception: = Exceptionhandler; End function Tmainform.handleappevent (aappevent:tapplicationevent; Acontext:tobject): Boolean; Begin case Aappevent of tapplicationevent.finishedlaunching:; Tapplicationevent.becameactive:; Tapplicationevent.willbecomeinactive:; Tapplicationevent.enteredbackground:; Tapplicationevent.willbecomeforeground:; Tapplicationevent.willterminate:; Tapplicationevent.lowmemory:; Tapplicationevent.timechange:; Tapplicationevent.openurl:; End Result: = True; End -See more At:http://codeverge.com/embarcadero.delphi.firemonkey/keep-application-on-top/2009583#sthash.6xm5jeui.dpuf
The tracked time triggers the log.
Formcreate
Formshow
Becameactive
Formsavestate
Enteredbackgrounbd
Willbecomeforeground
Becameactive
Formsavestate
Enteredbackgrounbd
Willbecomeforeground
Becameactive
Formsavestate
Enteredbackgrounbd
Willbecomeforeground
Becameactive
function Tfmxmusicplayerfrm.handleappevent (aappevent:tapplicationevent; Acontext:tobject): Boolean;var astate:string;begin case aappevent of Tapplicationevent.finishedlaunching: astate: = ' finishedlaunching '; Tapplicationevent.becameactive: astate: = ' becameactive '; Tapplicationevent.willbecomeinactive: astate: = ' willbecomeinactive '; Tapplicationevent.enteredbackground: astate: = ' enteredbackground '; Tapplicationevent.willbecomeforeground: astate: = ' willbecomeforeground '; Tapplicationevent.willterminate: astate: = ' willterminate '; Tapplicationevent.lowmemory: astate: = ' lowmemory '; Tapplicationevent.timechange: astate: = ' timechange '; Tapplicationevent.openurl: astate: = ' OpenURL '; End
Result: = True;end;
FMX. Platform.iOS.pas
Application delegates
Tapplicationdelegate = class{(toclocal, Uiapplicationdelegate)} private Fmainwindow:tfmxwindow; Public function application (sender:uiapplication; didfinishlaunchingwithoptions:nsdictionary): Boolean; overload; Cdecl Procedure application (sender:uiapplication; didreceivelocalnotification:uilocalnotification); overload; Cdecl Procedure application (sender:uiapplication; didregisterforremotenotificationswithdevicetoken:nsdata); overload; Cdecl function application (const OpenURL, sourceapplication:string; annotation:pointer): Boolean; overload; Cdecl Procedure applicationdidbecomeactive (const sender:uiapplication); Cdecl Procedure Applicationdidenterbackground (const sender:uiapplication); Cdecl Procedure Applicationdidregisterforremotenotificationswithdevicetoken (sender:uiapplication; Atoken:nsdata); Cdecl Procedure Applicationdidreceiveremotenotification (sender:uiapplication; Anotification:nsdictionary); Cdecl Procedure DidfailtoregiSterforremotenotificationswitherror (sender:uiapplication; AERROR:NSERROR); Cdecl Procedure applicationdidreceivememorywarning (sender:uiapplication); Cdecl Procedure Applicationsignificanttimechange (sender:uiapplication); Cdecl Procedure Applicationwillenterforeground (sender:uiapplication); Cdecl Procedure applicationwillresignactive (sender:uiapplication); Cdecl Procedure Applicationwillterminate (sender:uiapplication); Cdecl Procedure SetWindow (Window:uiwindow); Cdecl function Window:uiwindow; Cdecl Property Mainwindow:tfmxwindow read Fmainwindow; End
Application Delegatesfunction applicationdidfinishlaunchingwithoptions (self:id; _cmd:sel; Application:puiapplication; options:pnsdictionary): Boolean; Cdecl;begin Result: = PlatformCocoa.FAppDelegate.application (Tuiapplication.wrap (application), Tnsdictionary.wrap ( Options)); End;procedure applicationdidreceivelocalnotification (self:id; _cmd:sel; application:puiapplication; Notification:pointer); Cdecl;begin PlatformCocoa.FAppDelegate.application (Tuiapplication.wrap (application), Tuilocalnotification.wrap ( notification)); End;procedure didreceiveremotenotification (self:id; _cmd:sel; app:puiapplication; Anotification:pnsdictionary); Cdecl;begin PlatformCocoa.FAppDelegate.applicationDidReceiveRemoteNotification (Tuiapplication.wrap (APP), Tnsdictionary.wrap (anotification)); End;procedure Didfailtoregisterforremotenotificationswitherror (self:id; _cmd: SEL; App:puiapplication; ERROR:PNSERROR); Cdecl;begin PlatformCocoa.FAppDelegate.didFailToRegisterForRemoteNotificationsWiTherror (Tuiapplication.wrap (application), Tnserror.wrap (Error)); end;procedure Didregisterforremotenotificationswithdevicetoken (self:id; _cmd:sel; application:puiapplication; DeviceToken: Pnsdata); Cdecl;begin PlatformCocoa.FAppDelegate.applicationDidRegisterForRemoteNotificationsWithDeviceToken ( Tuiapplication.wrap (application), Tnsdata.wrap (Devicetoken)); end;procedure Applicationopenurlwithsourceannotation (self:id; _cmd:sel; application:puiapplication; url:pointer; sourceapplication:pnsstring; Annotation:id); var urlstring:string; Sourceappstring:string;begin if URL <> nil then urlstring: = Nsstrtostr (Tnsurl. Wrap (URL). absolutestring) Else urlstring: = '; If sourceapplication <> nil then sourceappstring: = Nsstrtostr (Tnsstring.wrap (sourceapplication)) Else Source Appstring: = "; PlatformCocoa.FAppDelegate.application (urlstring, sourceappstring, annotation); end;procedure Applicationdidbecomeactive (self:id; _cmd:sel; application:puiapplication); Cdecl;begin PlatformCocoa.FAppDelegate.applicationDidBecomeActive (Tuiapplication.wrap (application)); end; Procedure Applicationdidenterbackground (self:id; _cmd:sel; application:puiapplication); Cdecl;begin PlatformCocoa.FAppDelegate.applicationDidEnterBackground (Tuiapplication.wrap (application)); end; Procedure Applicationwillenterforeground (self:id; _cmd:sel; application:puiapplication); Cdecl;begin PlatformCocoa.FAppDelegate.applicationWillEnterForeground (Tuiapplication.wrap (application)); end; Procedure Applicationwillterminate (self:id; _cmd:sel; application:puiapplication); Cdecl;begin PlatformCocoa.FAppDelegate.applicationWillTerminate (Tuiapplication.wrap (application)); end;procedure Applicationdidreceivememorywarning (self:id; _cmd:sel; application:puiapplication); Cdecl;begin PlatformCocoa.FAppDelegate.applicationDidReceiveMemoryWarning (Tuiapplication.wrap (application)); end ;
How to handle the application events of the app