Replacing my (resource intensive) service

62 views Asked by At

I have an app that to displays new data in a notification. This data is provided by a network device. I can query this device and compare it the cached data in my service to determine if the notification needs to be updated. My app has a service that has local variables in which it caches the data. If the device has newer data, I present that in a notification to the user. Now I started to get IllegalStateException because Android O doesn't allow startService() gets called when the app is in background mode. I know I can start the service in Foreground mode, but since Android is providing less resource intensive ways, I would like to try something new. Next to the data being cached in local variables it gets stored in Firebase Database.

My requirements:

  1. Check every 10 seconds (if the screen is on) if there's new data
  2. Check if there's new data when the screen is switched on
  3. If there is new data, update the notification
  4. Needs to be long running and be able to compare new data to old (cached or from firebase) data.
  5. Can run when the app is in background mode

My thoughts: I've looked at Firebase job-dispatcher (https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-). Maybe I can configure it to run every time the screen is switched on, and every 10 seconds, to retrieve the new data from the network device and match that up with the data in Firebase database. But maybe it will cost a lot of performance to query the database that often.

2

There are 2 answers

0
Beuz On BEST ANSWER

Solved this by keeping my service. Android Oreo has forced me to use this service different. Just a short description in case anyone needs it:

  1. My service is put in foreground mode whenever a connection to the network device is possible.
  2. Service is stopped when no connection to the network device is possible. At the same time a Firebase Jobdispatcher job is scheduled.
  3. When the jobdispatcher runs the job, it looks if a connection to the device is possible. If true -> the service is started and promotes itself to the foreground.
  4. The service still maintains the connection with the network device and holds the objects that the notification needs.
1
Ashik sunny On

Try Object based Database like Realm, SnappyDB(Uber uses it) etc. Queries are super fast