android how to make listener to a custom variable?

后端 未结 2 1396
無奈伤痛
無奈伤痛 2021-02-01 10:26

i\'ve seen this thread : How to implement a listener about implement listeners.

its actually pretty simple but i don\'t get how exactly its done and how to implement in

2条回答
  •  忘掉有多难
    2021-02-01 10:26

    The part that you're missing it the class that actually notifies the listener. So you would need a class (most likely a service) that runs and pings the state of the network. Then when it detects a change it should call onStateChange() in any registered listeners. Then you would call setTheListener on that service, not on your activity.

    Here's a link that thoroughly describes this design pattern: http://en.wikipedia.org/wiki/Observer_pattern

提交回复
热议问题