Best practices
Always try to schedule BackgroundSync
When a user allows the background read permissions you should immediately call schedule
however you should also save
the user's acceptance in preferences, this will allow you to also call schedule
while the app is being launched:
class MyAplication : Application() {
override fun onCreate() {
super.onCreate()
if (userAllowedBackgroundSync) {
RookSamsungObject.schedule(this, enableLogs = isDebug)
}
}
}
This will allow to re-schedule the background sync in case that the system kill the process.
Prepare for strict battery scenarios
Some users want to have as much battery life as possible, so they have power saving mode always active, also some phone manufactures add their own energy restriction on top of Android's, this restrictions are mostly enabled by default and depending on the device will add a delay to background sync or even stopping it completely.
There are ways of preventing this, but this section will expand on the Battery Optimizations; there are 2 ways you can ask the user to disable battery optimizations:
- Use the
ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
intent to show the battery optimization settings screen to your users, because every Android Skin is different you will need to create instructions for every phone manufacturer. - A more straightforward approach would be using the
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent which displays a dialog with "Allow/Deny" options, however you will need to justify in the Play Console why is ignoring battery optimizations so important for your app.