0

I have a content observer detecting onChange successfully when the application is running in the foreground. I want to kill my application (swipe it away) but still have this content observer be able to detect onChange. I have tried many many things, but need help achieving the dream scenario.

  1. Start a foreground service (which forces a permanent notification), and registers the content observer in the foreground service. This method "works" but I really hate that permanent notification. My app currently work this way and I get complaints about the notification, thus my motivation to find a better method.

  2. Start a background service, keep it sticky. This does not work, because in newer version of Android, even sticky services gets killed after your main app gets killed

  3. Start a background service, on its onDestroy() method, send broadcast to restart the service. Basically read this guide. This doesn't really work either, my service restarts a couple of times, then I get errors which basically says I am trying to restart my service too many times.

  4. Start a background service, which starts a thread that runs forever. In the thread, do the content observer. I haven't tested this method yet, but it seem really bad because It will probably create orphaned threads, just seems really bad coding practice. Has anything tried something similar?

So basically the issue newer and newer Android is recycling services in almost all situations. How do I keep a background service running forever, because I need a content observer to run forever and listen to changes.

Other posts which i have read are this, this, this, this

Bqin1
  • 467
  • 1
  • 9
  • 19
  • 2
    "How do I keep a background service running forever" -- you don't on Android 8.0+. "because I need a content observer to run forever and listen to changes" -- use `JobScheduler` and [`addTriggerContentUri()`](https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#addTriggerContentUri(android.app.job.JobInfo.TriggerContentUri)) – CommonsWare Aug 02 '19 at 13:34
  • Thank you CommonsWare, however I noticed addTriggerContentUri() require minSDK version of 24, I even upgraded my android setup to AndroidX to try out workmanager, but found its addContentUriTrigger require min version 24 as well. Is there a way to backwards compatible this to version 14, or even 21? – Bqin1 Aug 03 '19 at 23:35
  • 1
    "Is there a way to backwards compatible this to version 14, or even 21? " -- no, the system stuff that supports this did not exist back then. There is no solution for this that will work across all devices, as many device manufacturers will terminate all of your processes when the user swipes away your task from the overview screen. Use `JobScheduler`/`WorkManager` on API Level 24+, and use a sticky background service prior to that (perhaps with `AlarmManager` occasionally starting your service in case it got stopped). – CommonsWare Aug 03 '19 at 23:52
  • I have no idea why the system would have a process for them, sorry. – CommonsWare Aug 08 '19 at 13:22

0 Answers0