-
Android 14 - Foreground Service types프로그래밍/Android 2023. 5. 30. 15:58반응형
Objective
The requirement to declare foreground service types allows you to clearly define the intent of the background work of your app while making it clear which use-cases are appropriate for foreground services.
Definition of background work
- Foreground Service
- Audio recording, working out tracking, navigation, or media playback, etc
- WorkManager
- Otherwise most of the time usage
Foreground Service types and permissions
Service types and permissions
- Service types and permissions were introduced in Android 10 (Reference).
Service types
- It has a purpose to specify that the service is a foreground service that satisfies a particular use case.
Service types permissions
- This is a normal permission, so the system automatically grants it to the requesting app.
- It must declare a specific permission based on the foreground service type.
- In addition, Google Play will be rolling out new policies to ensure the appropriate use of these APIs, with more details coming soon. (Reference)
- If apps that target Android 14 use a foreground service, it must specify appropriate foreground service types and permissions.
- (Details)
- If a use case in your app isn't associated with any of these types, we strongly recommend that you migrate your logic to use WorkManager or user-initiated data transfer jobs.
- The health, remoteMessaging, shortService, specialUse, and systemExempted types are new in Android 14.
System runtime checks
The system checks for proper use of foreground service types and confirms that the app has requested the proper runtime permissions or uses the required APIs. For instance, the system expects apps that use the foreground service type FOREGROUND_SERVICE_TYPE_LOCATION(location) type to request either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
How to declare foreground types, permissions
The following code snippet provides an example of a foreground service type declaration in the manifest:
<manifest ...> <!-- `FOREGROUND_SERVICE` permission is required for foreground services on OS 9+. --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- The service permission (`FOREGROUND_SERVICE_MEDIA_PLAYBACK`) is required for foreground services on OS 14+ (and targeted OS 14). --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" /> <application ...> <service android:name=".MyMediaPlaybackService" android:foregroundServiceType="mediaPlayback" android:exported="false"> </service> </application> </manifest>
- If an app that targets Android 14 doesn't define types for a given service in the manifest, then the system will raise MissingForegroundServiceTypeException upon calling startForeground() for that service.
- If you call startForeground() without declaring the appropriate foreground service type permission, the system throws a SecurityException.
Declare the types in the runtime
At runtime, if the foreground service only needs access to a subset of the types declared in the manifest, you can limit the service's access using the logic in the following code snippet:
Service.startForeground(notification, FOREGROUND_SERVICE_TYPE_LOCATION or FOREGROUND_SERVICE_TYPE_CAMERA)
If the foreground service type is not specified in the call, it will default to the values defined in the manifest.
Refers
- https://developer.android.com/about/versions/14/changes/fgs-types-required
- https://developer.android.com/guide/components/foreground-services
- https://support.google.com/googleplay/android-developer/answer/13392821?sjid=15553829868831539266-AP
- https://developer.android.com/guide/topics/manifest/service-element#foregroundservicetype
- https://support.google.com/googleplay/android-developer/answer/13315670?sjid=15553829868831539266-AP#foreground_service
반응형'프로그래밍 > Android' 카테고리의 다른 글
Android 14 - Additional restrictions on starting activities from the background (0) 2023.06.02 Android 14 - Apps can kill only their own background processes (0) 2023.05.31 Jetpack compose - Custom layout (0) 2022.12.12 Android 13 - Battery Resource Utilization (0) 2022.08.11 Android 13 - Programmable shaders (0) 2022.06.03 - Foreground Service