【发布时间】:2015-03-03 14:49:47
【问题描述】:
问题:我如何将我的应用程序保持在前台和/或防止其进入后台?
我创建了一个应用程序,并设法让它在设备启动时自动启动。该应用程序的目的是显示一些数据,除非我也想要它,否则永远不会进入菜单或终止。
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;
所以我想知道如何从这里开始。希望到目前为止我走在正确的轨道上。 :)
【问题讨论】:
-
我不认为你可以把它放在前台。如果你想重复执行某些任务,你可以使用一个服务,即使应用程序没有被使用,它也会在后台继续运行
-
应用程序每 xx 秒或每分钟刷新一次数据以显示新数据。但重要的是它也(总是)显示出来,而不仅仅是在后台可用。也许 TApplicationEvent 不是正确的方法,也许还有其他选项可以让我的应用保持活跃...
-
您可以做的是在后台运行服务,当有新数据可用时,您可以向用户显示通知,并且当用户单击通知时会显示所需的数据。就像所有新闻和其他应用程序正在做的那样。
-
当这些信息应该在没有交互的情况下可用时,我是否可以让我的应用程序对这个通知做出反应,并保持应用程序“活跃”,这行得通吗?我希望有更多选择,因为我也不希望收到显示有新数据的通知。
-
您可以有一个处理程序,它会以特定的时间间隔对您的服务器执行 ping 操作以获取新数据。但即使在这种情况下,您也无法将您的应用置于前台,除非用户对您的应用执行了某些操作。
标签: android delphi firemonkey delphi-xe7