1. 程式人生 > >Firemonkey裏觸發home按鍵被按下的事件

Firemonkey裏觸發home按鍵被按下的事件

fin nts 問題 login 搜索 解決 assign numbers 自動

吾八哥我最近在使用Delphi裏的Firemonkey平臺寫一個叫“由由密碼管家”的APP工具,是跨多平臺的,如ios/android/windows/macOs。由於是用於密碼管理的,那麽在手機裏操作會很頻繁的被按下home鍵而切換到後臺的,所以希望程序被按下home鍵的時候隱藏到後臺就自動鎖定程序,再激活APP的時候要求重新輸入密碼才可以操作。那麽問題來了,在Firemonkey裏面如何捕獲按下home鍵的事件呢?網上搜索各種資料,終於找到了答案,這裏分享出來具體的解決方法:

function TMaster.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin case AAppEvent of TApplicationEvent.FinishedLaunching: ; TApplicationEvent.BecameActive: ; TApplicationEvent.WillBecomeInactive: begin if not Assigned(LoginFrame) then ShowFrame(TLoginFrame, Master, False); LoginFrame.BringToFront;
end; TApplicationEvent.EnteredBackground: ; TApplicationEvent.WillBecomeForeground: ; TApplicationEvent.WillTerminate: ; TApplicationEvent.LowMemory: ; TApplicationEvent.TimeChange: ; TApplicationEvent.OpenURL: ; end; Result := True; end
;

當然,在使用前需要在 onCreate 中獲取消息接口

使用IFMXApplicationEventService接口裏的SetApplicationEventHandler方法將上述事件方法作為參數進行調用即可實現,HandleAppEvent方法裏會觸發各種程序相關的事件通知。代碼如下:

var
  SvcEvents: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(SvcEvents)) then
  begin
    SvcEvents.SetApplicationEventHandler(HandleAppEvent);
  end;
end;

Firemonkey裏觸發home按鍵被按下的事件