在我找到了问题的解决方法到底有没有打盹模式,通过com.android.internal.location.GpsLocationProvider的源代码,看完之后我注意到,在发送的com.android.internal.location.ALARM_WAKEUP意图防止位置提供者打瞌睡。因此,为了保持GPS从打瞌睡我转播意向每隔10秒,我说我的服务类以下内容:
[...]
private Handler handler;
private PowerManager powerManager;
private PendingIntent wakeupIntent;
private final Runnable heartbeat = new Runnable() {
public void run() {
try {
if (isRecording && powerManager != null && powerManager.isDeviceIdleMode()) {
LOG.trace("Poking location service");
try {
wakeupIntent.send();
} catch (SecurityException | PendingIntent.CanceledException e) {
LOG.info("Heartbeat location manager keep-alive failed", e);
}
}
} finally {
if (handler != null) {
handler.postDelayed(this, 10000);
}
}
}
};
@Override
public void onCreate() {
handler = new Handler();
wakeupIntent = PendingIntent.getBroadcast(getBaseContext(), 0,
new Intent("com.android.internal.location.ALARM_WAKEUP"), 0);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TrackService");
[...]
}
我意识到什么打瞌睡是,但是它的_my_设备,我应该能够声明可以将我的电池用于对我有用的目的,在这种情况下记录GPS轨道。这也是一个不常见的用例,例如runkeeper和strava需要这样做。阻止设备进入休眠状态似乎是一种破解,并且不得不打开屏幕来打击旨在节省电池寿命的东西似乎倒退了回来。 –
@KorneliusElstner如果您正在使用设备运行,它将不会进入休眠模式。 “只要用户通过移动设备来唤醒设备,......系统就会退出打盹状态,所有应用程序都会恢复正常活动。” – mattm