Android Tutorial Automatic Foreground Mode

Status
Not open for further replies.
This is a new feature added in B4A v8.0.

Starting from Android 8.0, services cannot be simply started while the app is in the background or not running at all. Background means that there isn't any visible activity or any service in foreground state (Service.StartForeground).

Common cases where we expect services to start even if the app is not in the foreground:

- Push notifications
- Services scheduled with StartServiceAt or StartServiceAtExact
- Services set to start after boot with #StartAtBoot attribute
- Home screen widgets

The only way for services to start when the app is not in the foreground is with a new API that ensures that the service will start and immediately switch to foreground mode.
This is now handled automatically in the B4A framework. When needed services automatically start in foreground mode.
Needed = Running on Android 8+ and the app is considered in the background when the service is started.

You need to do one thing which is to stop the automatic foreground mode when the service completed its task.
This can be done in two ways:

1. Call Service.StopAutomaticForeground. This will stop the automatic foreground mode if the service was in that mode. Otherwise it will not do anything.
It is safe to call it multiple times. The service will not be immediately destroyed.

2. Call StopService(Me).

If you don't stop the automatic foreground mode then a notification icon will be left after the task completed.

For example if you are receiving push notifications:
B4X:
Sub Service_Start (StartingIntent As Intent)
   fm.HandleIntent(StartingIntent)
   Sleep(0) 'allow the MessageReceived event to be raised.
   Service.StopAutomaticForeground
End Sub

Widgets:
B4X:
Sub Service_Start (StartingIntent As Intent)
   rv.HandleWidgetEvents(StartingIntent)
   Sleep(0)
   Service.StopAutomaticForeground
End Sub

Note that the foreground icon will not be visible at all when the task completes almost immediately.

Service.AutomaticForegroundMode - This is a new property that you can set in Service_Create to override the default behavior.
The possible values are:

Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED - This is the default value. The service will enter foreground mode when needed as described above.

Service.AUTOMATIC_FOREGROUND_NEVER - It will never enter foreground mode. This means that you must call Service.StartForeground when needed or your app will crash.

Service.AUTOMATIC_FOREGROUND_ALWAYS - Service will always start in foreground mode, on all versions of Android. This can be useful if you want to make sure that the app is not killed until the task completes. Note that you should also acquire a partial wake lock.

Another new property is Service.AutomaticForegroundNotification. This allows you to change the notification that appears in automatic foreground mode.

Services life cycle in Android 8+

This is not related to automatic foreground mode however it is another thing that you should be aware of when working with services in Android 8+.
Up until Android 7, the OS never killed the app services. Instead when the app was in the background it killed the complete process. Service_Destroy was never called in those cases.

Starting from Android 8, when the app is in the background (no activities and no foreground services), the OS can kill services without killing the process. In those cases the Service_Destroy sub will be called.
This means for example that we can no longer assume that the starter service is always running. It will be started when the process starts however later it can be destroyed.
You can call StartService or CallSubDelayed whenever an activity is resumed to make sure that the relevant service is started.
Note that process global variables are tied to the process and will not be destroyed when the service is stopped.

Update: Starting from B4A v8.3 the starter service will never be killed unless the whole process is killed. This means that you can always assume in your code that the starter service is running.
It is done internally by changing the starter service context to the application context.
 
Last edited:

KMatle

Expert
Licensed User
Longtime User
What does "a partial wake lock" mean?

You can use the search function of the forum and/or Google. However:

To save energy, Android has different "sleep modes". You can set "locks" so that the device does not shut off all of the needed system services (otherwise your app won't do anything because the whole device is "sleeping" and system services will be stopped)

1.JPG
 
Status
Not open for further replies.
Top