[英]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
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.