http://blog.sina.com.cn/s/blog_44fa172f0102vwr2.html

直接上代码,还有条经验就是SetApplicationEventHandler可注册多个事件方法。

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.

还可以参考下面的例子,是消息的类型

FMX.Platform.TApplicationEvent

http://docwiki.embarcadero.com/Libraries/Seattle/en/FMX.Platform.TApplicationEvent

http://docwiki.embarcadero.com/Libraries/Tokyo/en/FMX.Platform.TApplicationEvent

TApplicationEvent = (FinishedLaunching, BecameActive, WillBecomeInactive, EnteredBackground, WillBecomeForeground, WillTerminate, LowMemory, TimeChange, OpenURL);

An instance of TApplicationEvent may have 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, but your application is still running in the background.

Supported

Supported

FinishedLaunching

Your application has been launched.

Supported

Supported

LowMemory

This warns your application that the device is running out of memory.

Your application should reduce memory usage, freeing structures and data that can be loaded again at a later point.

Supported

Supported

OpenURL

You application has received a request to open an URL.

Application events of this type are usually associated with a context. This context is an instance of the iOS-only TiOSOpenApplicationContext class, which provides the following read-only properties:

  • TiOSOpenApplicationContext.SourceApp is a string that contains 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 API reference documentation for more information.

 

Supported

TimeChange

There has been a significant change in time.

This event might happen for example when the day changes or when the device changes to or from daylight savings time.

 

Supported

WillBecomeForeground

The user is now using your application, which was 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.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

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
( 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 :
;//第一次运行app触发,从后台切换过来也触发
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

和IOS的对比

 
/app启动完毕调用,应用初次启动
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//进入后台时调用:一般在这里保存应用数据(数据,比如暂停游戏)
- (void)applicationDidEnterBackground:(UIApplication *)application 连续点击两次Home按钮
在任务栏点击SpringBoard或者按下Home按钮,单次点击Home按钮

- (void)applicationWillResignActive:(UIApplication *)application

//程序回到时调用,恢复数据
- (void)applicationWillEnterForeground:(UIApplication *)application
//接收内存警告时候调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
//程序即将退出
- (void)applicationWillTerminate:(UIApplication *)application
//程序获取焦点,在任务栏中回到app
- (void)applicationDidBecomeActive:(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

跟踪的时间触发日志。

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 delegates

function 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
SourceAppString := '';
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;
 

如何处理App的Application的事件的更多相关文章

  1. FMX App的Application的事件(各种手机的全局按键)

    直接上代码,还有条经验就是SetApplicationEventHandler可注册多个事件方法. unit Unit6; interface uses  System.SysUtils, Syste ...

  2. WinForm中Application.Idle事件用法

    Application.Idle 事件 描述:当应用程序完成处理并即将进入空闲状态时发生.如果您有必须执行的任务在线程变为空闲之前,请将它们附加到此事件. public partial class F ...

  3. 深入delphi编程理解之消息(五)重写(override)dispatch、wndproc方法和Application.OnMessage事件

    dispatch.wndproc是VCL framework在TWinCtronl定义的虚拟方法,下面程序通过重写(override)这两函数拦截WM_LBUTTONDOWN消息,来与Applicat ...

  4. 追踪app崩溃率、事件响应链、Run Loop、线程和进程、数据表的优化、动画库、Restful架构、SDWebImage的原理

    1.如何追踪app崩溃率,如何解决线上闪退 当 iOS设备上的App应用闪退时,操作系统会生成一个crash日志,保存在设备上.crash日志上有很多有用的信息,比如每个正在执行线程的完整堆栈 跟踪信 ...

  5. iOS 获取app进程被杀死事件

    程序被用户双击上滑杀死后,就对app做一些特殊的处理 下面的方法可以获取到用户双击上滑杀死的事件 - (void)applicationDidEnterBackground:(UIApplicatio ...

  6. MySQL 5.6 主从复制如何处理——触发器,函数,存储过程,调度事件

      截图来自MySQL5.6的pdf版文档. 说明: 1)基于语句的复制时,trigger会在slave上执行,所以slave上也需要有trigger的定义,不然会导致主从数据不一致的: 2)基于行的 ...

  7. spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持

    spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...

  8. 移动web app开发必备 - zepto事件问题

    问题描述: 项目在祖先元素上绑定了 touchstart,touchmove,touchend事件,用来处理全局性的事件,比如滑动翻页 正常状态下: 用户在子元素上有交互动作时,默认状态下都是会冒泡到 ...

  9. Android基于mAppWidget实现手绘地图(九)–如何处理地图对象的touch事件

    为了响应touch事件,需要设置OnMapTouchListener 示例: private void initMapEventsListener() { mapWidget.setOnMapTouc ...

随机推荐

  1. Spark之 Spark Streaming整合kafka(Java实现版本)

    pom依赖 <properties> <scala.version>2.11.8</scala.version> <hadoop.version>2.7 ...

  2. VMware Ubuntu 窗口太小 未安装VMwareTools

    安装vmtools就行了:在虚拟机点击重新安装vmtools. 在虚拟机里点开挂载的,将那个压缩文件拉出document,解压 在命令行sudo运行.pl结尾的文件,一路回车就行了. 重启即可.

  3. python作业(第十一周)基于RabbitMQ rpc实现的主机管理

    作业需求: 可以对指定机器异步的执行多个命令 例子: >>:run "df -h" --hosts 192.168.3.55 10.4.3.4 task id: 453 ...

  4. jquery获取当前屏幕宽度

    参考文章 <script type="text/javascript"> $(document).ready(function() { alert($(window). ...

  5. 用HttpClient发送HTTPS请求报SSLException: Certificate for <域名> doesn't match any of the subject alternative names问题的解决

    最近用server酱-PushBear做消息自动推送,用apache HttpClient做https的get请求,但是代码上到服务器端就报javax.net.ssl.SSLException: Ce ...

  6. iOS - 组件化探究之私有库的创建

    http://www.cocoachina.com/ios/20180511/23359.html

  7. 两个应用之间传递广播的规则 Broadcast

    sendBroadcast(new Intent(Config.ACTION_PRINT),”com.qf.permission.print”);先判断应用有没有对应的权限 再去判断有没有对应的act ...

  8. JSP标准标签库JSTL

    1.什么是JSTL? JSP标准标签库(JSP Standard Tag Library) 2.JSTL标准标签库中的常用标签 JSTL是JSP页面的标签库,实质上是一段Java代码.我们常用的是它的 ...

  9. tomcat执行文件权限

    .当我在linux下某个目录执命令或者安装的时候通常会提示没有权限或者不可以操作,这时需要加权限 chmod /usr/local/tomcat/bin; 2关于LINUX权限(启动tomcat)-b ...

  10. Spark cache、checkpoint机制笔记

    Spark学习笔记总结 03. Spark cache和checkpoint机制 1. RDD cache缓存 当持久化某个RDD后,每一个节点都将把计算的分片结果保存在内存中,并在对此RDD或衍生出 ...