如何在 iOS 和 Android 上获取应用程序恢复状态?

2024-06-25

当应用程序恢复时,是否可以从代码角度检查某些内容iOS and Android?

例如当应用程序最小化并恢复时(应用程序仍在设备后台运行)。


你需要使用IFMX应用程序事件服务 http://docwiki.embarcadero.com/Libraries/XE8/en/FMX.Platform.IFMXApplicationEventService注册一个回调,应用程序将收到通知:

uses FMX.Types, FMX.Platform;

function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
  case AAppEvent of
    TApplicationEvent.FinishedLaunching:    Log.d('Launched.');
    TApplicationEvent.BecameActive:         Log.d('Gained focus.');
    TApplicationEvent.EnteredBackground:    Log.d('Now running in background.');
    TApplicationEvent.WillBecomeForeground: Log.d('Restoring from background.');
    TApplicationEvent.WillBecomeInactive:   Log.d('Going to lose focus.');
    TApplicationEvent.WillTerminate:        Log.d('Quitting the application.');
    TApplicationEvent.LowMemory:            Log.d('Device running out of memory.');

    // iOS only
    TApplicationEvent.TimeChange:           Log.d('Significant change in time.');
    TApplicationEvent.OpenURL:              Log.d('Request to open an URL.');
  end;

  Result := True;
end;

procedure TForm11.FormCreate(Sender: TObject);
var
  aFMXApplicationEventService: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService,
    IInterface(aFMXApplicationEventService))
  then
    aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
  else
    Log.d('Application Event Service not supported.');
end;

有关事件类型的更多信息here http://docwiki.embarcadero.com/Libraries/XE8/en/FMX.Platform.TApplicationEvent.

A 好文章 http://community.embarcadero.com/blogs/entry/mobile-app-lifecycle-events-handling-in-delphi-xe5-40067就该主题而言帕韦乌·格沃瓦茨基(对于Delphi XE5,但仍然有用)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 iOS 和 Android 上获取应用程序恢复状态? 的相关文章

随机推荐