0

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

1 Answers1

0

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.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • yeah.. but we can't use fcm. but how apps like whatsapp keep their service in background ? –  Jun 03 '18 at 13:44
  • @user332863 They don't keep constant connection. They also rely on FCM. However apps like whatsapp are eligible to make themselves as whitelisted apps. You can check this [document](https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases) which describes how to whitelist your app. – Sagar Jun 04 '18 at 02:40
  • @user332863 have you checked my answer from this [SO](https://stackoverflow.com/questions/49566426/can-i-still-have-a-data-logging-background-service-in-android-8-0/49568651#49568651) & this [SO](https://stackoverflow.com/questions/49639229/run-background-services-on-battery-saver-mode-android/49641208#49641208) – Sagar Jun 04 '18 at 02:46
  • so in that case we have to relay on some polling mechanism right? because if we use WS connection, and OS terminate the service we might miss events. if we use jobscheduler how often we can call our polling request .(Our use case only work if network is active, so we can leverage Jobschedulers constraints api) –  Jun 04 '18 at 06:15
  • @user332863 the minimum period required for JobScheduler is around 15 minutes i.e. your job will be only executed once per 15 minutes. Behaviour in Doze mode is OS dependent and you have no control over it. You can use AlarmManager which can be triggered at specific time – Sagar Jun 04 '18 at 06:21
  • this 15 limit is only in doze mode right? so other wise we can combine AlarmManager and JobScheduler to perform connection with in specific interval right ? –  Jun 04 '18 at 12:55
  • @user332863us `this 15 limit is only in doze mode right?` No. When you use Periodic job the restriction is regardless of Doze Mode. You can use it if its not critical, ohterwise use AlarmManager to trigger request at particular time. You can also try to whitelist your app. – Sagar Jun 04 '18 at 13:14