Android Wear:测量传感器并防止环境模式/睡眠 [英] Android Wear: measuring sensors and preventing ambient mode / sleep

查看:166
本文介绍了Android Wear:测量传感器并防止环境模式/睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Android Wear开发了一个应用程序,该应用程序需要连续测量一个小时左右的运动(加速度计)传感器才能收集数据.在操作过程中,我遇到了设备进入环境模式(或睡眠)并减慢(或关闭)传感器速度的问题. 结果,我正在使用以下命令来锁定屏幕,并且它似乎可以正常工作.但这似乎也很浪费,因为我实际上不需要打开屏幕,只需运行传感器即可.

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. 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

当前,我在主要活动中具有onSensorsChanged()方法.我愿意将其放在服务中,但是据我了解,如果它仍位于主UI线程中,将无济于事.我可以将其放在自己的线程中,但是我不确定这会导致环境模式降低传感器的速度. 问题: 1)有没有办法防止环境模式?还是检测到将其关闭? 2)如果传感器在各自的服务/线程中,我可以让活动进入睡眠状态并仍保持传感器全速收集吗? 3)如果传感器在各自的服务/线程中,我是否仍可以通过dataapi将数据发送到手机? 谢谢.

CUrrently, I have the onSensorsChanged() method in the main activity. 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. 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? 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? 3) If the sensors are in their own service/thread, can I still send data over the dataapi to the phone? Thanks.

推荐答案

1)使用唤醒锁.为了使CPU保持唤醒状态,但让屏幕变暗,可以使用如下代码:

1) Use a wake lock. To keep the CPU awake but let the screen go dim, you can use code like the following:

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

其中TAG是表示保持唤醒锁的组件的字符串,而duration以毫秒为单位.

where TAG is a string to indicate the component holding the wake lock, and duration is measured in milliseconds.

我认为我不需要警告您避免使设备进入环境的电池不良影响.一个普通的Wear设备可能会持续一整个小时,也可能不会持续一整个小时.

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.

2)是.这是Service的一种定义,一个应用程序组件代表一个应用程序希望在不与用户交互的同时执行长时间运行的操作的愿望"(来自

2) Yes. 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).

3)是.我在许多应用程序中都这样做;不需要Data API调用必须位于UI线程上.

3) Yes. I do this in a number of apps; there's no requirement that Data API calls need to be on the UI thread.

这篇关于Android Wear:测量传感器并防止环境模式/睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆