Reputation: 164
Question: How do I keep my application in the foreground and/or prevent it from going to the background ever?
I have created an application and managed to get it to automatically start when the device has booted. The purpose of the app is to show some data and never ever go to the menu or terminate unless I want it too.
Every now and then the ActivityManager decides that my app is not important enough and wants to send it to the background or even worse, kill it entirely. Sometimes it happens after 1, 4 or 16 hours.
So far I have found out how to check if the app receives various ApplicationEvents with:
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;
So I would like to know how to proceed from here. Hopefully I was on the right track so far. :)
Upvotes: 1
Views: 4436
Reputation: 469
I dont think you can keep it in foreground. If you wants to perform some task repeatedly you can use a Service which will keep on running in background even if the app is not in use.
What you can do is run a Service in the background and when new data is available you can show the notification to the user and when user clicks on the notification the required data is displayed. Like all the news and other apps are doing.
Plus, you can have a Handler which pings your server for new data at particular intervals. But even in that case you wont be able to bring your app to the foreground until or unless user performs some action with your app.
Upvotes: 3