android service notify activity completed best way?

后端 未结 3 1359
悲哀的现实
悲哀的现实 2020-12-30 10:49

I created a service which syncs data from the web on a background thread and want to notify a list activity when the service has completed so it can update it\'s cursor? Wh

3条回答
  •  一个人的身影
    2020-12-30 11:47

    If you're already using the support library, you could just as easily fire a broadcast from the service using the LocalBroadcastManager back to your activity that would listen for the broadcast being sent.

    Using LocalBroadcastManager ensures only your own application would ever receive the broadcast so you don't have to worry about leaking private data or opening up potential security holes.

    Also see: how to use LocalBroadcastManager?

    EDIT (09/2014):

    A better way to do this would be to use an event bus framework like Otto (my favourite) or GreenRobot/EventBus to avoid coupling your components too tightly.

提交回复
热议问题