我如何将我的应用程序保留在前台



问题:如何将我的应用程序保持在前台和/或防止它进入后台?

我已经创建了一个应用程序,并设法让它在设备启动时自动启动。该应用程序的目的是显示一些数据,除非我也想要,否则永远不会进入菜单或终止。

ActivityManager不时会认为我的应用程序不够重要,并想将其发送到后台,甚至更糟的是,将其完全销毁。有时会在1、4或16小时后发生。

到目前为止我已经找到了如何检查应用程序是否接收到各种ApplicationEvents:

function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
  case AAppEvent of
    TApplicationEvent.FinishedLaunching: MemoPrint('Finished Launching');
    TApplicationEvent.BecameActive: MemoPrint('Became Active');
    TApplicationEvent.WillBecomeInactive: MemoPrint('Will Become Inactive');
    TApplicationEvent.EnteredBackground: MemoPrint('Entered Background');
    TApplicationEvent.WillBecomeForeground: MemoPrint('Will Become Foreground');
    TApplicationEvent.WillTerminate: MemoPrint('Will Terminate');
    TApplicationEvent.LowMemory: MemoPrint('Low Memory');
  end;
  Result := True;
end;
procedure TForm1.LinkAppEvent;
var
  aFMXApplicationEventService: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService,
    IInterface(aFMXApplicationEventService)) then
    aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
  else
    MemoPrint('Application Event Service is not supported.');
end;

所以我想知道如何从这里开始。希望到目前为止我走在了正确的轨道上。:)

我认为你不能把它放在前台。如果你想重复执行一些任务,你可以使用一个服务,即使应用程序没有使用,它也会在后台继续运行。

您可以做的是在后台运行服务,当有新数据可用时,您可以向用户显示通知,当用户单击通知时,将显示所需的数据。就像所有的新闻和其他应用程序一样。

此外,您还可以有一个处理程序,它会在特定的时间间隔为服务器ping新数据。但即使在这种情况下,你也无法将你的应用程序带到前台,除非用户对你的应用进行了一些操作。

最新更新