Search code examples
androidandroid-serviceandroid-jobschedulerandroid-background

Android migrate background service to jobscheduler


we have a project which uses background service to retrieve real-time events from server. we use s websocket connection to retrieve data. but now in Android Oreo or higher, os starts to showing battery Warning.

my concern is that if we migrate to jobscheduler does it stop after sometime. we want to show a notification if event happen

i tried job scheduler and it offers recurring invokes. but our connection is real-time so if jobscheduler stops we don't get data. so how other apps handle this (example WhatsApp )

what is the best way to keep our connection live and retrieve data ?

thanks advance


Solution

  • my concern is that if we migrate to jobscheduler does it stop after sometime.

    Yes. During doze mode all the scheduled jobs are deferred until maintenance window is made available by the OS.

    what is the best way to keep our connection live and retrieve data ?

    You shouldn't maintain a persistent connection with your server and permanently run background service. Starting from Android O you won't be able to run Services in background. OS will terminate all the services and any attempts to start service in background will result in IllegalStateException

    Your requirement seems that you need to use FCM. The basic idea is, instead of constantly asking server for data, let server notify the app about data availability. For more details and implementation steps, refer to official document.