再谈Delphi 10.4.2 FMX应用程序事件TApplicationEvent

再谈Delphi 10.4.2 FMX应用程序事件TApplicationEvent

 

 

直接上个以前的测试代码,一看就明白:

function TfmxMainUI.AppEvent(
  AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
//:AContext是一个提供附加上下文的对象。通常事件的类型是足够的信息,AContext为null。
var
  LAppEventStatus :string;
begin
  Result := true;
  case AAppEvent of
    TApplicationEvent.FinishedLaunching:
      begin
        if FifMemo then Memo1.Lines.Add('应用FinishedLaunching已完成载入到操作系统:');
      end;
    TApplicationEvent.WillTerminate:
      begin
        if FifMemo then Memo1.Lines.Add('应用WillTerminate用户正在通过操作系统退出你的应用');
      end;
    TApplicationEvent.EnteredBackground:
      begin
        //:也许10.4 for Android10越来越严格约束:
        //:按了返回键若程序不控制则立即进入后台:
        //:而进入后台:主程序就被:=nil掉了
        //:(nil就相当于对象 TObject.disposeOf = FreeAndNil(TObject)  )
        //:FreeAndNil是10.4与10.3.3的一个变化
        //TimerGlobal.Enabled := false;
        //TimerGuangGao.Enabled := false;
        ///已进入后台千千万不与UI有任何交互,否则异常:
          //if FifMemo then Memo1.Lines.Add('应用已进入后台');
          //Text_0201.Text:=TInterlocked(FRestSearchFrequency).ToString;
        //EnteredBackground:用户在手机设置的超时时间内一直不在使用您的应用程序,
        //:但您的应用程序仍在后台运行。
      end;
    TApplicationEvent.WillBecomeInactive:
      begin //:WillBecomeInactive:应用将要失去焦点:不活动:即将进入后台运行状态EnteredBackground
        //TimerGlobal.Enabled := false;
        if (FDMem_CustomerAppID.Active=false) then LAppEventStatus:='应用WillBecomeInactive:内存表不活动'
        else LAppEventStatus:='应用WillBecomeInactive:但内存表活动';
        if FifMemo then Memo1.Lines.Add('测试内存表状态:'+sLineBreak+LAppEventStatus);
        {if FifMemo then Memo1.Lines.Add(
          'App切入后台再重新(或最开始全新)调入过程:'+sLineBreak
          +'1、应用将变为非活动状态:'+sLineBreak
          +string('').PadLeft(8,#32)+'此时就需提前关闭任何'+sLineBreak
          +string('').PadLeft(8,#32)+'与UI相关的线程与计时,'+sLineBreak
          +string('').PadLeft(8,#32)+'否则手机重新切到前台'+sLineBreak
          +string('').PadLeft(8,#32)+'会内部异常!');
        if FifMemo then Memo1.Lines.Add(
          '*注意:已进入后台事件中:'
          +string('').PadLeft(8,#32)+'千万不能有与UI相关的'+sLineBreak
          +string('').PadLeft(8,#32)+'任何交互、线程与计时'+sLineBreak
          +string('').PadLeft(8,#32)+'否则手机重新切到前台'+sLineBreak
          +string('').PadLeft(8,#32)+'亦会内部异常!'+sLineBreak
          );}
      end;
    TApplicationEvent.WillBecomeForeground:
      begin
        if (FDMem_CustomerAppID.Active=false) then LAppEventStatus:='应用将从之前的后台状态进入前台:但内存表不活动'
        else LAppEventStatus:='应用将从之前的后台状态进入前台:内存表活动';
        if FifMemo then Memo1.Lines.Add('2、应用WillBecomeForeground将进入前台,测试是否杀死内存表:'+LAppEventStatus);
        //TimerGlobal.Enabled := true;
        //TimerGuangGao.Enabled := true;
      end;
    TApplicationEvent.BecameActive:
      begin //:BecameActive:应用获得了焦点:活动:
        //TimerGlobal.Enabled := true;
        if (FDMem_CustomerAppID.Active=false) then LAppEventStatus:='应用获得了焦点:但内存表不活动'
        else LAppEventStatus:='应用获得了焦点:内存表活动';
        if FifMemo then Memo1.Lines.Add('3、应用BecameActive已变为活动状态:'+LAppEventStatus);
      end;
    TApplicationEvent.TimeChange:
      begin
        if FifMemo then Memo1.Lines.Add('应用TimeChange计时的时间上发生了重大改变:');
        //时间上发生了重大变化。
        //例如,当日期更改或设备更改为夏令时或夏令时,可能会发生此事件。
      end;
    TApplicationEvent.LowMemory:
      begin
        if FifMemo then Memo1.Lines.Add('操作系统报应用程序LowMemory高内存耗用:');
        //这会警告应用程序设备内存不足。
        //您的应用程序应该减少内存使用量,释放可以在以后再次加载的结构和数据。
      end;
    TApplicationEvent.OpenURL:
      begin
        if FifMemo then Memo1.Lines.Add('iOS App接受了一个URL的请求:');
      end;
  end;

end;

原理:

//unit FMX.Platform;

  { Application events }

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

  TApplicationEventHelper = record helper for TApplicationEvent
  const
    aeFinishedLaunching = TApplicationEvent.FinishedLaunching deprecated 'Use TApplicationEvent.FinishedLaunching';
    aeBecameActive = TApplicationEvent.BecameActive deprecated 'Use TApplicationEvent.BecameActive';
    aeWillBecomeInactive = TApplicationEvent.WillBecomeInactive deprecated 'Use TApplicationEvent.WillBecomeInactive';
    aeEnteredBackground = TApplicationEvent.EnteredBackground deprecated 'Use TApplicationEvent.EnteredBackground';
    aeWillBecomeForeground = TApplicationEvent.WillBecomeForeground deprecated 'Use TApplicationEvent.WillBecomeForeground';
    aeWillTerminate = TApplicationEvent.WillTerminate deprecated 'Use TApplicationEvent.WillTerminate';
    aeLowMemory = TApplicationEvent.LowMemory deprecated 'Use TApplicationEvent.LowMemory';
    aeTimeChange = TApplicationEvent.TimeChange deprecated 'Use TApplicationEvent.TimeChange';
    aeOpenURL = TApplicationEvent.OpenURL deprecated 'Use TApplicationEvent.OpenURL';
  end;

  TApplicationEventData = record
    Event: TApplicationEvent;
    Context: TObject;
    constructor Create(const AEvent: TApplicationEvent; AContext: TObject);
  end;
  TApplicationEventMessage = class (System.Messaging.TMessage)
  public
    constructor Create(const AData: TApplicationEventData);
  end;

  TApplicationEventHandler = function (AAppEvent: TApplicationEvent; AContext: TObject): Boolean of object;

  IFMXApplicationEventService = interface(IInterface)
    ['{F3AAF11A-1678-4CC6-A5BF-721A24A676FD}']
    procedure SetApplicationEventHandler(AEventHandler: TApplicationEventHandler);
  end;

unit System.Messaging;

type

  /// Base class for all messages
  TMessageBase = class abstract;
  TMessage = TMessageBase;
  {$NODEFINE TMessage} // Avoid ambiguity with 'Winapi.Messages.TMessage'

  TMessage = class (TMessage)
  protected
    FValue: T;
  public
    constructor Create(const AValue: T);
    destructor Destroy; override;
    property Value: T read FValue;
  end;

 

 

 

你可能感兴趣的:(FireMonkey,Delphi,10.4.2,Delphi应用程序事件,AppEvent,FMX,AppEvent,FMX应用程序事件)