如何创建在打瞌睡模式和省电模式下都能工作的 PhoneStateListener?
How to create PhoneStateListener that works in both Doze Mode and Power Saving Mode?
当设备进入打盹模式或省电模式时,我的 PhoneStateListener 停止工作。我还需要在设备重启时注册 PhoneStateListener。我不想使用 BroadcastReceiver,因为不再推荐使用它,请参阅 https://developer.android.com/about/versions/oreo/background#broadcasts
我认为目前不推荐使用 BroadcastReceiver 不是正确的定义,它只是有更多 limitations 例如在清单中和根据文档为隐式广播注册广播接收器:
- Apps can continue to register for explicit broadcasts in their manifests
- Apps can use
Context.registerReceiver()
at runtime to register a receiver for any broadcast, whether implicit or explicit.
打瞌睡模式几乎没有 restrictions 但它提供了在打瞌睡模式和节能模式下使用 AlarmManager
触发警报的解决方案:
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle()
or setExactAndAllowWhileIdle()
.
您应该 setAndAllowWhileIdle and setExactAndAllowWhileIdle 并决定哪一个与您相关。
当设备进入打盹模式或省电模式时,我的 PhoneStateListener 停止工作。我还需要在设备重启时注册 PhoneStateListener。我不想使用 BroadcastReceiver,因为不再推荐使用它,请参阅 https://developer.android.com/about/versions/oreo/background#broadcasts
我认为目前不推荐使用 BroadcastReceiver 不是正确的定义,它只是有更多 limitations 例如在清单中和根据文档为隐式广播注册广播接收器:
- Apps can continue to register for explicit broadcasts in their manifests
- Apps can use
Context.registerReceiver()
at runtime to register a receiver for any broadcast, whether implicit or explicit.
打瞌睡模式几乎没有 restrictions 但它提供了在打瞌睡模式和节能模式下使用 AlarmManager
触发警报的解决方案:
If you need to set alarms that fire while in Doze, use
setAndAllowWhileIdle()
orsetExactAndAllowWhileIdle()
.
您应该 setAndAllowWhileIdle and setExactAndAllowWhileIdle 并决定哪一个与您相关。