OnigeworkRequest(内容观察者)关闭应用程序后失去了第一张图片。可以为后续图片工作
我正在尝试在用户媒体上实现可靠的后台监听器(例如,如果用户拍照,我想捕获它,即使应用程序已关闭)。我正在使用 Android 的 WorkManager (OneTimeWorkRequest)。
当应用程序位于任务栏的前台或后台时,一切正常。 但是,当我关闭应用程序时,不会为用户拍摄的第一张照片调用 doWork()
。随后的图片,第二张,第三张等等都工作得很好。
当应用程序关闭时,拍摄第一张照片后,我可以在 Logcat 中看到很多其他内容,几乎就像 Android 重建应用程序上下文、初始化一些内容一样,但实际上不运行 doWork()
,事件丢失。
第一个后台事件触发后的日志:
...
2022-03-31 11:56:24.842 10616-10616/com.my.app I/s.app: The ClassLoaderContext is a special shared library.
2022-03-31 11:56:24.998 10616-10616/com.my.app D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-03-31 11:56:24.998 10616-10616/com.my.app D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-03-31 11:56:25.000 10616-10616/com.my.app D/WM-WrkMgrInitializer: Initializing WorkManager with default configuration.
2022-03-31 11:56:25.008 10616-10616/com.my.app I/TetheringManager: registerTetheringEventCallback:com.my.app
2022-03-31 11:56:26.793 10616-10632/com.my.app W/System: A resource failed to call close.
我感觉这个问题与 WorkManager 初始化有关。它似乎在第一个事件期间没有正确初始化。它通过默认初始化程序随时初始化,但由于未正确调用 doWork()
,它会丢失事件。
知道为什么会这样以及如何克服这个问题吗?
代码示例
https://github.com/tomekit/so_71691180
按顺序重现:
- 在 Android Studio 中打开它,在模拟器上编译并安装应用程序。
- 打开相机并拍照,您应该在 LogCat 中看到:
I/ContentURI: content://media/external/images/media/109
。 - 关闭应用程序(滑开)并确保它在任务栏中不再可用。
- 拍张照片。您将看到没有生成任何日志。
- 再拍一张照片。现在您可以看到日志已正确创建,这意味着 doWork() 已被执行。任何后续照片都会正确创建日志条目。
I am trying to implement reliable background listener on user media (e.g. if user takes a picture, I want to capture that, even if application is closed). I am using Android's WorkManager (OneTimeWorkRequest).
Everything works fine when application is at the foreground or in the background in the task bar.
However when I close the application, doWork()
doesn't gets called for the first picture that user is taking. Subsequent pictures, 2nd, 3rd and so on work just fine.
When app is closed, after first picture is taken, I can see plenty additional stuff in Logcat, almost like if Android rebuilt the app context, initialize some stuff, but actually doesn't run the doWork()
, event gets lost.
Logs after first background event triggered:
...
2022-03-31 11:56:24.842 10616-10616/com.my.app I/s.app: The ClassLoaderContext is a special shared library.
2022-03-31 11:56:24.998 10616-10616/com.my.app D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-03-31 11:56:24.998 10616-10616/com.my.app D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2022-03-31 11:56:25.000 10616-10616/com.my.app D/WM-WrkMgrInitializer: Initializing WorkManager with default configuration.
2022-03-31 11:56:25.008 10616-10616/com.my.app I/TetheringManager: registerTetheringEventCallback:com.my.app
2022-03-31 11:56:26.793 10616-10632/com.my.app W/System: A resource failed to call close.
I get the feeling that this issue has something related to WorkManager initialization. It seems it's not properly initialized during first event. It gets initialized on the go by default initializer, however it loses the event as doWork()
isn't called properly.
Any idea why this might be and how to overcome this issue?
CODE EXAMPLE
https://github.com/tomekit/so_71691180
In order to reproduce:
- Open this in Android Studio, compile and install app on emulator.
- Open camera and take a photo, you should see:
I/ContentURI: content://media/external/images/media/109
in LogCat. - Close an app (swipe away) and make sure it's no longer available in taskbar.
- Take a photo. You will see no log is produced.
- Take another photo. Now you can see that logs are created correctly, meaning that doWork() gets executed. Any subsequent photo, creates log entry correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论