XE5: Does my Android app will need a TTimer to timeout and force it to close? What I mean is that when I go to the home and dont return the app keep going running.

XE5: Does my Android app will need a TTimer to timeout and force it to close? What I mean is that when I go to the home and dont return the app keep going running.

Other question, same subject: how can I prevent the OS to kill my task when in bg? Can we develop a system service with Delphi that will/can start the app?

Comments

  1. >>Other question, same subject: how can I prevent the OS to kill my task when in bg?
    you can't, the os is the supervisor, it can pause your app, kill it, make it it's slave, modify code at runtime, prevent access, anything it wants really...

    ReplyDelete
  2. You're suppose to suspend all unnecessary activities in your app when it loses focus (even for short periods), so as to conserve battery.

    You're not supposed to close it, the OS will do that when memory gets low, if it doesn't, your app should wait in its suspended state so that it can be brought back into focus faster if the user switches tasks.

    It's possible to develop Android services, but they're intended for background tasks (such as handling notifications, tracking geolocation, etc.)

    ReplyDelete
  3. AFAIK currently services cannot be written with delphi

    ReplyDelete
  4. Thank you for that.
    So, best technique is to force close after a while when user goes home and dont returns, I say that because my app depends on accessing database to retrieve its info.

    ReplyDelete
  5. Force close shouldn't be required. Just act like an Android app. Stop accessing DB stuff and other activities when you head to the background. Start up again when you come to the foreground.

    Use something like this to detect switching to/from background, no?

    uses
      FMX.Platform, FMX.Types;
    ...
        function ApplicationEventHandler(AAppEvent: TApplicationEvent;
          AContext: TObject): Boolean;
    ...
        AppEvents: IInterface;
    ...
    function TMainForm.ApplicationEventHandler(AAppEvent: TApplicationEvent;
      AContext: TObject): Boolean;
    begin
      Log.d('', Self, 'ApplicationEventHandler', Format('%s',
        [GetEnumName(TypeInfo(TApplicationEvent), Integer(AAppEvent))]));
      Result := True;
      case AAppEvent of
        aeWillBecomeForeground, aeBecameActive: ForegroundMode := True;
        aeWillBecomeInactive, aeEnteredBackground: ForegroundMode := False;
      end;
    end;
    ...
      if TPlatformServices.Current.SupportsPlatformService(
           IFMXApplicationEventService,
           AppEvents) then
        AppEvents.SetApplicationEventHandler(
          ApplicationEventHandler);

    ReplyDelete
  6. Thank you the code! It will be very usefull

    ReplyDelete
  7. Event type "aeEnteredForeground" should be into FMX.Platform? I put that to run

    ReplyDelete
  8. Oops. My bad. Let me just edit the code posted in that comment...

    There, try that.

    ReplyDelete

Post a Comment