问题
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?
回答1:
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().
来源:https://stackoverflow.com/questions/44543572/fileobserver-listening-on-background-android-o