简体   繁体   English

即使我有PARTIAL_WAKE_LOCK,在屏幕关闭时也无法写入SDCard

[英]Not able to write to SDCard while screen is off even though I have PARTIAL_WAKE_LOCK

I'm totally new to Java and Android, so I think I'm probably missing something obvious. 我对Java和Android完全陌生,所以我想我可能缺少明显的东西。 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. 我正在编写一个用于对方向传感器进行采样并将结果写入SDCard上的文件,并将其显示在屏幕上的日志中的应用程序。 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. 由于我也想在屏幕变暗时进行操作,因此我添加了代码来获取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. 据我所知,当屏幕变暗时,仍在对传感器进行采样(使我认为唤醒锁起作用),但是没有任何结果写入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. 但是,当我查看在SD卡上创建的文件时,记录在文件中的唯一过渡就是屏幕打开时发生的过渡。 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. 至于我写文件的方式,我尝试过(我想我是说对了...)包装在BufferedWriter中的FileWriter和包装在FileOutputStream中的FileWriter。 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. 最初,即使我有了唤醒锁,方向传感器也无法正常工作,直到我了解到为Intent.ACTION_SCREEN_OFF添加BroadcastReceiver的方法,并在获得它后注销了方向监听器,然后重新注册了它。 Do I need to "unregister" the FileWriter and "re-register" it or something? 我需要“取消注册” FileWriter并“重新注册”它或其他吗? 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. 奇怪的是,输出文件包含在收到Intent.ACTION_SCREEN_OFF之前的所有方向更改,并且在我为屏幕重新上电时收到了在Intent.ACTION_SCREEN_ON之后的所有方向更改,但是之间没有任何变化。

I'm writing for Android 1.6 (I have a G1) My project is set to use SDK 4 我正在为Android 1.6(我有一个G1)编写项目,我的项目设置为使用SDK 4

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. 文档中还有用于外部存储BroadcastReceiver的代码。 You could use that to wait until storage is available before writing. 您可以使用它来等到存储可用之前再进行写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM