NotificationListenerService锁屏后停止接收通知的原因及非前台服务场景下的解决方案咨询
NotificationListenerService锁屏后停止接收通知的原因及非前台服务场景下的解决方案咨询
我在NotificationListenerService中添加了日志用于调试,代码如下:
class NotificationListenerService : NotificationListenerService() { override fun onDestroy() { logi("on Destroy") super.onDestroy() } override fun onConfigurationChanged(newConfig: Configuration) { logi("on config change") super.onConfigurationChanged(newConfig) } override fun onRebind(intent: Intent?) { logi("on rebind") super.onRebind(intent) } override fun onBind(intent: Intent?): IBinder? { return super.onBind(intent) logi("onBind") } override fun startService(service: Intent?): ComponentName? { logi("startService") return super.startService(service) } override fun onUnbind(intent: Intent?): Boolean { logi("on Unbind") return super.onUnbind(intent) } override fun onListenerConnected() { logi("on Connected") super.onListenerConnected() } override fun onListenerDisconnected() { logi("on Disconnected") super.onListenerDisconnected() } override fun onListenerHintsChanged(hints: Int) { logi("on Hints changed") super.onListenerHintsChanged(hints) } override fun onNotificationPosted(sbn: StatusBarNotification, rankingMap: RankingMap) { logi("on posted") super.onNotificationPosted(sbn, rankingMap) } override fun onNotificationPosted(sbn: StatusBarNotification) { logi("on posted") super.onNotificationPosted(sbn) } }
目前所有必要权限都已授予,并且我把应用的电池优化设置改成了**“无限制”**。屏幕处于点亮状态时,服务工作完全正常;但屏幕熄灭后,过5~10秒服务就会“罢工”:此时发送新通知不会触发任何日志输出,可一旦我再次点亮屏幕,之前未显示的日志会一次性全部打印出来。
我感到疑惑的是:如果服务出现解绑、断开连接或被销毁的情况,应该会输出on Unbind、on Disconnected这类日志,但实际并没有这类日志产生,说明服务的绑定状态是正常的。那到底是什么原因导致了这种现象?有没有不需要使用前台服务的解决办法?谢谢!
内容来源于stack exchange
相关产品推荐
相关产品推荐

