问题描述
我的应用程序中有FileObserver
,现在它在后台运行,并且如果注册了新文件-实例化将其上传到前台服务中的某些服务器.
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.
在Android O中,我们应该使用FirebaseJob Dispatcher在后台执行某些工作,但是如何将其应用于FileObserver
?有什么方法可以在后台分析数据?还是从现在起无法使用FileObserver
?
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?
推荐答案
你不能.
Android 8.0(O)并没有改变.创建一个粘性前台服务,然后继续保持不可靠性,因为您的进程仍将永远无法运行.此外,还可以让用户抱怨您的应用程序一直在运行.
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.
使用FileObserver
从未如此可靠,因为Android可以通过用户选择(例如,设置"中的强制停止")随时终止任何进程或释放系统RAM.使用粘性前台服务离拥有持久的服务/流程非常近,甚至永远也不会持久.
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并未对此进行任何更改.
Android 8.0 has not changed any of this.
根据您的用例,您可以尝试切换到JobScheduler
并使用它来监视MediaStore
Uri
通过addTriggerContentUri()
.
Depending on your use case, you could try switching to JobScheduler
and using it to monitor a MediaStore
Uri
via addTriggerContentUri()
.
这篇关于FileObserver在后台监听,Android O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!