问题
I'm developing an app that tracks trips. After searching about this matter, I concluded that the best way to achieve this (continuously track the user's location) is to use a foreground service. In some cases is working quite well, but in some other cases (even with DOZE off), I get some time intervals in which the phone is completely still and I stop getting sensor data (either accelerometer or locations from fused location provider).
This problem got worse when I tried it in an Android 9 device, (i guess) due to the new restrictions introduced by it (no sensor data when the phone is idle, etc.). To solve this issue, or at least try to reduce its effects, I thought about implementing a wakelock/job/alarm just to fire with a 1-minute frequency and do nothing. It might be a really bad idea but I´ve been struggling with this for a long time I dont know what to do anymore.
Thank you very much in advance!
回答1:
From your post
I get some time intervals in which the phone is completely still and I stop getting sensor data (either accelerometer or locations from fused location provider).
If the phone is still, why do you want to get any location or sensor data from tracking purposes?
回答2:
Use a WakeLock
. This is its intended purpose.
EDIT
To clarify: Your device most likely ceases collecting data because it is going to sleep. Under some circumstances, this behavior might be attributed to Doze, but you mentioned you are using a foreground service. According to a comment by Dianne Hackborn in this post, apps with running foreground services are not restricted by Doze. And, according to the docs, WakeLocks
are an appropriate method of keeping the device awake.
来源:https://stackoverflow.com/questions/54352024/guarantee-that-android-keeps-on-retrieving-sensor-data