【问题标题】:onNotificationPosted() method not called when the app is running in background应用程序在后台运行时未调用 onNotificationPosted() 方法
【发布时间】:2015-05-20 16:07:24
【问题描述】:

我编写了一个应用程序来读取到达 android 设备 (API>=19) 的通知并使用 NotificationListenerService 的 onNotificationPosted() 方法部分。只有当我在屏幕上保持应用程序处于活动状态时,我才能阅读发布的通知。无论如何我可以让应用程序在后台运行并且仍然使用 onNotificationsPosted 方法来检索通知。

谢谢!

【问题讨论】:

    标签: android android-notifications android-notification-bar


    【解决方案1】:

    让你的 Service 的 onStartCommand 返回 START_STICKY 标志

    http://developer.android.com/reference/android/app/Service.html

    【讨论】:

      【解决方案2】:

      我已经在使用默认在后台运行的 NotificationServiceListener。错误在于广播接收器。我已在暂停时取消注册广播,这是该应用在后台运行时无法获取通知的原因。只需在 onDestroy 中取消注册即可。即使应用程序被终止,另一种保留已发送广播的有趣方法是使用粘性广播。

      【讨论】:

        猜你喜欢
        • 2019-02-06
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 2015-07-23
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 2017-04-22
        相关资源
        最近更新 更多