如何處理App的Application的事件

來源:互聯網
上載者:User

標籤:rap   html   inter   case   sop   android   oar   uiwindow   index   

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.TApplicationEventFMX.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.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      ( 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 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    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的事件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.