Android Tutorial Android 14 / targetSdkVersion 34 and Services

B4A v13.0+ should be used with targetSdkVersion 34+.

Android 14 continues the longtime trend of making services less flexible and more difficult to use (converging to iOS background tasks features from 2014).
Many of the previous use cases for services are better covered by receivers: Receivers and Services in 2023+

Another common use case for services was to overcome the activities complex lifecycle. This is not relevant if using B4XPages, where a single activity is used and it is not paused. For non-B4XPages projects, you can continue to use the starter service for such cases. Don't use regular services as they will be killed when the app is in the background.

We are mostly left with foreground services. Example: a music app that wants to continue playing while the app is in the background. By having a foreground service, the OS will not kill the process.

Starting from Android 14 foreground services must have a defined foreground service type.
The list of types is available here: https://developer.android.com/about/versions/14/changes/fgs-types-required
The suitability of the selected type and the app will/might be checked by Google Play review team (thus it is less relevant for non-Google Play apps).

Continuing the example of a music app: https://developer.android.com/about/versions/14/changes/fgs-types-required#media
We need to add two things to the manifest editor - permission and service type:
B4X:
AddPermission(android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK)
SetServiceAttribute(MyMusicService, android:foregroundServiceType, "mediaPlayback")
Note that all of these permissions are non-dangerous (no need to request at runtime).

There is a general "short service" type which gives your app about 3 minutes to run in the background. This can be useful for downloads or other tasks that shouldn't be interrupted.
You can read about its requirements here: https://developer.android.com/about/versions/14/changes/fgs-types-required#short-service
A new timeout event is now available and it is raised when the OS requests the service to stop being foreground:
B4X:
'This is only needed if you have declared the foreground service type to be shortService!
Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(NotificationId)
End Sub
Not calling StopForeground will cause the app to crash.
This event will only be raised on Android 14+.

There is also a "special use" type which is flexible but requires Google Play reviewers approval: https://developer.android.com/about/versions/14/changes/fgs-types-required#special-use
 
Last edited:

scsjc

Well-Known Member
Licensed User
Longtime User
Is it necessary to use for the starter service:

SetServiceAttribute(Starter, android:foregroundServiceType, shortService)?
 

Alessandro71

Well-Known Member
Licensed User
Longtime User

does this mean that we must add that code to our existing Service modules?
 

dieterp

Active Member
Licensed User
Longtime User
My app is a live scoring app for cricket, and it uses a service that runs a timer every 20 seconds and then uploads the latest scores to a database. What is going to be my best Service Type to use in this instance? I can't stop and start the service every 3 minutes because a lot happens when you start the service.

What is the best way to keep the service running once you start it?
 

Alexander Stolte

Expert
Licensed User
Longtime User
Always create a new thread for questions.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…