3

I have FileObserver in my application, now it runs on the background and if new file registered - instantiate uploading of it to some server in foreground service.

In Android O we should use FirebaseJob Dispatcher to do some job in background, but how could we apply it for FileObserver? Is there any way to analyze data in background? Or maybe it's fail to use FileObserver since now?

A. Shevchuk
  • 2,109
  • 14
  • 23

1 Answers1

3

but how could we apply it for FileObserver?

You can't.

Is there any way to analyze data in background?

That has not changed with Android 8.0 (O). Create a sticky foreground service, then live with the unreliability, as your process still will not run forever. Also, live with the user complaints that your app is running all of the time.

Or maybe it's fail to use File Observer since now?

Using FileObserver has never been reliable, as Android can terminate any process at any time, by user choice (e.g., "Force Stop" in Settings) or to free up system RAM. Using a sticky foreground service is as close as you can get to having an everlasting service/process, and even it will not last forever.

Android 8.0 has not changed any of this.

Depending on your use case, you could try switching to JobScheduler and using it to monitor a MediaStore Uri via addTriggerContentUri().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for you answer. Well, other thing that I need to support API lvl 19... It's impossible with JobInfo.Builder and addTriggerContentUri() was added only in API 24. – A. Shevchuk Jun 14 '17 at 11:56
  • @A.Shevchuk: "Well, other thing that I need to support API lvl 19" -- perhaps use `JobScheduler` on API Level 24+, and use your unreliable `FileObserver` approach on the older devices. – CommonsWare Jun 14 '17 at 12:04
  • well, seems like pain is comming after this... It would be hard to support two different mechanism to deal with the same things in common way... I'd better replace 'FileObserver' with something better, but seems like I have no choice for now. I'll try your tips, thanks! – A. Shevchuk Jun 14 '17 at 12:18