简体   繁体   English

Android Wear:测量传感器并防止环境模式/睡眠

[英]Android Wear: measuring sensors and preventing ambient mode / sleep

I have built an app for Android Wear that needs to measure the motion (accelerometer) sensors continuously for an hour or so for data collection purposes. 我已经为Android Wear开发了一个应用,该应用需要一个小时左右的时间连续测量运动(加速度计)传感器以用于数据收集。 During operation, I have had problems with the device going into ambient mode (or sleep) and slowing down (or shutting off) the sensors. 在操作过程中,我遇到了设备进入环境模式(或睡眠)并减慢(或关闭)传感器速度的问题。 As a result, I am using the following command to lock the screen on and it seems to work. 结果,我正在使用以下命令来锁定屏幕,它似乎可以正常工作。 But it also seems wasteful, since I dont actually need the screen on, just the sensors running. 但这似乎也很浪费,因为我实际上不需要打开屏幕,只需运行传感器即可。

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 。getWindow()clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Allow going to sleep //允许睡觉

CUrrently, I have the onSensorsChanged() method in the main activity. 目前,我在主要活动中具有onSensorsChanged()方法。 I am willing to put it in a service, but from what I understand that won't help if it is still in the main UI thread. 我愿意将其放在服务中,但据我了解,如果它仍位于主UI线程中,将无济于事。 I could put it in its own thread, but I'm not sure that that will present Ambient Mode from slowing the sensors. 我可以将其放在自己的线程中,但是我不确定这会导致环境模式降低传感器速度。 Questions: 1) Is there way to prevent ambient mode? 问题:1)有没有办法防止环境模式? Or if detected to turn it off? 还是检测到将其关闭? 2) If the sensors are in their own service/thread, can I let the activity go to sleep and still maintain sensor collection at full rate? 2)如果传感器在各自的服务/线程中,我可以让活动进入睡眠状态并仍保持传感器全速收集吗? 3) If the sensors are in their own service/thread, can I still send data over the dataapi to the phone? 3)如果传感器在各自的服务/线程中,我是否仍可以通过dataapi将数据发送到手机? Thanks. 谢谢。

1) Use a wake lock. 1)使用唤醒锁。 To keep the CPU awake but let the screen go dim, you can use code like the following: 为了使CPU保持唤醒状态,但让屏幕变暗,可以使用如下代码:

PowerManager powerMgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wakeLock.acquire(duration);

where TAG is a string to indicate the component holding the wake lock, and duration is measured in milliseconds. 其中TAG是一个字符串,用于指示持有唤醒锁的组件, duration以毫秒为单位。

I assume I don't need to warn you about the adverse battery effects of keeping the device from going into ambient. 我假设我不需要警告您有关使设备避免进入环境的不利电池影响。 An average Wear device may or may not last for the solid hour you're proposing. 一个普通的Wear设备可能会持续一整个小时,也可能不会持续一整个小时。

2) Yes. 2)是的。 This is kind of the definition of a Service , "an application component representing either an application's desire to perform a longer-running operation while not interacting with the user" (from https://developer.android.com/reference/android/app/Service.html ). 这是Service一种定义,“一个应用程序组件,表示应用程序希望在不与用户交互的情况下执行长时间运行的操作”(来自https://developer.android.com/reference/android/app /Service.html )。

3) Yes. 3)是的。 I do this in a number of apps; 我在许多应用程序中都这样做; there's no requirement that Data API calls need to be on the UI thread. 不需要Data API调用必须位于UI线程上。

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

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