简体   繁体   English

Android-服务在后台运行

[英]Android - Service running in background

I am quite new to Android programming and I wonder what the best approach would be to keep information updated while the screen is off. 我对Android编程非常陌生,我不知道最好的方法是在屏幕关闭时保持信息更新。

The specific situation is as follows: - I use a service that extends Service and implements SensorEventListener. 具体情况如下:-我使用扩展Service并实现SensorEventListener的服务。 - I use the accelerometer to check if the user is sitting or standing (onSensorChanged). -我使用加速度计来检查用户是坐着还是站着(onSensorChanged)。 - If the user have been sitting for a specified time the device vibrates and beeps. -如果用户已经坐了指定的时间,则设备会振动并发出蜂鸣声。

The problem is: - The service don't update at a frequency that makes the alarm go of i time. 问题是:-服务不会以使警报响起的频率更新。 Sometimes is is late by a couple of minutes, sometimes it just updates as I turn the device-screen on. 有时会迟到几分钟,有时会在我打开设备屏幕时更新。 - The update frequency seems to vary between devices. -设备之间的更新频率似乎有所不同。

My question is: - What would be the best approach to solve the problem? 我的问题是:-解决问题的最佳方法是什么? Maybe a service isn't the best solution. 也许服务不是最好的解决方案。

My intention is to keep the service running a long time in the background. 我的意图是使服务在后台长时间运行。

Also, I do not want to use a Wake lock because of the battery (which is an important factor in the situation). 另外,由于电池(这是这种情况的重要因素),我也不想使用唤醒锁。

// Victor //维克多

I mentioned this problem in this blog . 我在这个博客中提到了这个问题。 I had faced this problem like you. 我像你一样面对这个问题。 I just found a workaround which I write but it does not work every device. 我刚刚找到了我写的解决方法,但它不适用于所有设备。 Device's policy for sensors depends on manufacturer and manufacturer limits sensors on cheap phones. 传感器的设备策略取决于制造商,并且制造商会限制廉价手机上的传感器。 ( I don't know why but i guess it is about battery usage) (我不知道为什么,但是我想这是关于电池的使用)

If you want to use sensors you should use wakelock.(If there is a solution with not using wakelocks, i want to try it) 如果要使用传感器,则应使用唤醒锁(如果有不使用唤醒锁的解决方案,我想尝试一下)

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

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