Show/Hide Toolbars

Android Applications

Navigation: Android Specifics > Android "Service"

Running Service in Foreground

Scroll Prev Top Next More

 

Foreground/Background on Android

 

A good sample Demo for Android Local services is

GitHub\Object Pascal\Multi-Device Samples\Device Sensors and Services\Foreground Location Tracking

 

https://developer.android.com/about/versions/oreo/background-location-limits

 

In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently an app can retrieve the user's current location while the app is running in the background. Under these conditions, apps can receive location updates only a few times each hour.

 

In Unbind

JavaService.startForeground(NotificationId, GetNotification);

 

Monitor Main Activity State Via HandleApplicationEvent

case ApplicationEvent of

   TApplicationEvent.WillBecomeForeground:

   TApplicationEvent.EnteredBackground:

 

 

https://developer.android.com/about/versions/10/features#fg-service-types

 

Implementation note

 

Android 10 requires the foreground service type to be declared in the AndroidManifest.xml file. For this reason, this demo application uses the AndroidManifest.template.xml file as a workaround for adding the foregroundServiceType attribute in the service declaration:

 

<service android:name="com.embarcadero.services.ForegroundLocationTrackingService"

   android:exported="false"

   android:foregroundServiceType="location" />

 

Implemented by replacing the <%services%> in the application template with the above text.