打盹模式 android,android – 在打盹模式下获取位置更新

最后,我通过阅读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");

[...]

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值