android - 每 10 10 分钟获取一次光照水平
我的应用程序需要在其后台服务中具有更新的光级别值(10-15 分钟的延迟就可以了)(这不是商业应用程序)......
我知道如何使用 SensorEventListener 检索光值并且SensorManager 但在 API 文档中明确指出,如果您在不需要监听器时不取消注册监听器,它将在短短几个小时内耗尽电池电量。
现在我的问题是......我如何使用侦听器和传感器管理器每 10-15 分钟检索一次光照水平值?
我可以使用类似下面的计时器来每 10-15 分钟运行一次此任务吗?
private TimerTask lightSensorTimer = new TimerTask() {
@Override
public void run() {
if (sensorManager != null) {
Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor != null) {
sensorManager.registerListener(sensorListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
while (newLightLevel == lightLevel) {
try {
**Thread.sleep(1000);**
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted exception in lightsensor timer: " + e.getMessage());
}
}
PhoneEvents.this.lightLevel = PhoneEvents.this.newLightLevel;
sensorManager.unregisterListener(sensorListener, lightSensor);
}
}
}
};
Sensoreventlistener 仅执行此操作:
private SensorEventListener sensorListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
PhoneEvents.this.newLightLevel = event.values[0];
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
};
我什至不确定是否可以在计时器任务中使用 Thread.sleep。但我已经准备好 TimerTasks 需要快速完成,否则它们会批量化。
对于这样的事情有更清洁的解决方案吗?
谢谢
问候, 安德烈亚斯
My app needs to have an updated light level value (a 10-15 minute delay is fine or something) (this is not a commercial app) within its background service....
I know how to retrieve the light value using a SensorEventListener and the SensorManager but in the API docs it says explicitly that if you don't unregister the listener when you don't need it it will drain your battery in just a few hours.
now my question is....how can I use the listener along with the sensor manager to retrieve a light level value every 10-15 minutes?
Could I use something like the below with a Timer that runs this task every 10-15 minutes?
private TimerTask lightSensorTimer = new TimerTask() {
@Override
public void run() {
if (sensorManager != null) {
Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor != null) {
sensorManager.registerListener(sensorListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
while (newLightLevel == lightLevel) {
try {
**Thread.sleep(1000);**
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted exception in lightsensor timer: " + e.getMessage());
}
}
PhoneEvents.this.lightLevel = PhoneEvents.this.newLightLevel;
sensorManager.unregisterListener(sensorListener, lightSensor);
}
}
}
};
The sensoreventlistener does nothing but this:
private SensorEventListener sensorListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
PhoneEvents.this.newLightLevel = event.values[0];
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
};
I'm not even sure if you can use Thread.sleep within a timer task. But I have ready that TimerTasks need to finish quickly otherwise they batch up.
Is there a cleaner solution for something like this?
Thanks
Regards,
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用调用 serviceIntent 的警报。
每 15 分钟启动一次警报:
您的服务如下所示:
You should use an alarm that calls a serviceIntent.
Start the alarm every 15 minutes:
and your service looks like this: