简体   繁体   English

如果已收到来自服务的消息,则通知活动

[英]Notify Activity if a message has been received from Service

Hello Please i need help, 您好,我需要帮助,

How Can I notify my activity if a message has been received from a GCMListener service ruuning in background. 如果已从后台中断的GCMListener服务接收到消息,如何通知我的活动。

First, bear in mind that you may not have an activity around. 首先,请记住,您周围可能没有活动。 Android may have terminated your process (eg, user pressed HOME and time elapsed), and your newly-created process may just have your GCM-handling service. Android可能已经终止了您的进程(例如,用户按下HOME键并经过了时间),而您新创建的进程可能仅具有您的GCM处理服务。

That being said, you can use an event bus: 话虽如此,您可以使用事件总线:

  • LocalBroadcastManager
  • greenrobot's EventBus greenrobot的EventBus
  • Square's Otto 广场的奥托
  • etc. 等等

Have the service post an event on the bus. 让服务在总线上发布事件。 Have your activity set up to receive the event. 设置您的活动以接收事件。 If the service posts the event, and the event is unhandled, the service knows that the UI is not around and can perhaps do something else with the message (eg, raise a Notification , save the message in a database for later use). 如果服务发布了事件,并且未处理事件,则服务会知道UI不在,并且可以对消息进行其他操作(例如,引发Notification ,将消息保存在数据库中以备后用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从B活动通知A列表数据已被更改? - How to notify from B activity to A that list data has been changed? 通知服务活动 - Notify activity from service 当列表行中的任何值已从任何活动更改时如何通知适配器 - how to notify adapter when any value in the listrow has been changed from any activity 如何通知一个活动在片段中按下了后退按钮? - How to notify an Activity that the back button has been pressed in a Fragment? 如何使活动通知片段已按下后退按钮 - How to have an Activity notify a Fragment that the back button has been pressed Android:如何通知应用程序服务已卸载 - Android: how notify an app that a service has been uninstalled 在Activity被杀死后,GCM推送持续存储的数据 - Persisting data received by GCM push after Activity has been killed 如何通知清单活动其详细说明活动中的一项已更新? - How to notify list activity that one of its items has been updated in detail activity? 服务未收到活动的本地广播 - Local broadcast from Service not received by Activity 接收消息收到来自GCM服务的确认 - Receving Message received Acknwoledgement From GCM Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM