即使我有 PARTIAL_WAKE_LOCK,屏幕关闭时也无法写入 SDCard
我对 Java 和 Android 完全陌生,所以我想我可能遗漏了一些明显的东西。 我正在编写一个应用程序,在其中对方向传感器进行采样并将结果写入 SDCard 上的文件,并将其显示在屏幕上的日志中。 屏幕打开时一切正常。由于我还想在屏幕变暗时进行操作,因此我添加了代码来获取 PARTIAL_WAKE_LOCK。 据我所知,当屏幕变暗时,传感器仍在采样(让我认为唤醒锁起作用了),但没有任何结果写入 SDCard。我认为这就是正在发生的事情,因为当我通过按菜单键重新唤醒屏幕时,屏幕上的日志显示了屏幕关闭时发生的所有转换。但是,当我查看 SD 卡上创建的文件时,文件中记录的唯一转换是屏幕打开时发生的转换。 至于我如何编写文件,我已经尝试过(我想我说得对......)一个包裹在 BufferedWriter 中的 FileWriter 和一个包裹在 FileOutputStream 中的 FileWriter。 起初,即使我有唤醒锁,方向传感器也无法工作,直到我学会了一种解决方法,为 Intent.ACTION_SCREEN_OFF 添加了 BroadcastReceiver,当我得到它时,我取消注册方向侦听器,然后重新注册它。 我是否需要“取消注册”FileWriter 并“重新注册”它或其他什么? 奇怪的是,输出文件包含接收到 Intent.ACTION_SCREEN_OFF 之前的所有方向更改,以及当我重新启动屏幕时接收到 Intent.ACTION_SCREEN_ON 之后的所有方向更改,但两者之间没有任何变化。
我正在为 Android 1.6 编写(我有 G1) 我的项目设置为使用 SDK 4
I'm totally new to Java and Android, so I think I'm probably missing something obvious.
I'm writing an app where I sample the orientation sensor and write the result to a file on the SDCard as well as display it in a log on the screen.
Everything works fine while the screen is on. Since I want to also operate while the screen goes dark, I added code to acquire a PARTIAL_WAKE_LOCK.
From what I can tell, when the screen goes dark the sensors are still being sampled (making me think the wake lock worked), but none of the results are written to the SDCard. I think this is what is happening, because when I reawaken the screen by pressing the menu key, the log on the screen has all the transitions displayed that took place while the screen was off. But, when I look at the file created on the SD card, the only transitions recorded in the file are the ones that occurred while the screen was on.
As for how I'm writing the file I have tried (I think I'm saying this right...) a FileWriter wrapped in BufferedWriter, and a FileWriter wrapped in a FileOutputStream.
At first, the orientation sensor wasn't working even though I had the wake lock until I learned a work around where I added a BroadcastReceiver for Intent.ACTION_SCREEN_OFF and when I get it, I unregister the orientation listener, then re-register it.
Do I need to "unregister" the FileWriter and "re-register" it or something?
The weird part is that the output file contains all orientation changes before the Intent.ACTION_SCREEN_OFF is received, and all of them after Intent.ACTION_SCREEN_ON is received when I power the screen back up, but nothing in between.
I'm writing for Android 1.6 (I have a G1)
My project is set to use SDK 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否按照此处所述检查外部存储是否可用?那时它可能根本无法写入。文档中还有代码用于外部存储存储广播接收器。您可以使用它来等待存储可用后再写入。
Are you checking if external storage is available as described here? It may be simply unwritable at that time. There's also code in the docs for a external storage BroadcastReceiver. You could use that to wait until storage is available before writing.