How can I send result data from Broadcast Receiver to Activity

后端 未结 4 706
刺人心
刺人心 2020-12-06 02:26

I have an Activity that calls a Broadcast Receiver. The Broadcast Receiver waits and listens to GPS. When the listener gets the new point I want to send that new point to Ac

4条回答
  •  借酒劲吻你
    2020-12-06 02:53

    you have couple of ways you can do it and several considerations.

    1. you can poll, meaning check every now an again using either Handler or Timer to see if info has arrived.

    2. you can register the broadcast receiver as an inner class of your activity and then you can call methods in your activty.

    3. you can have the Broadcast send Intent to your class with the info, but if your activity is not in foreground you might bring it there , and that's not 100% what you want...

    Regarding some consideration, BroadCastReciver is mainly used as a listener, not notider so inner class, is best practice, in my opinion, for use with Activities, for Services you can use it as a standalone class and register it in the Manifest.xml... Now you got to remember that when broadcast is being broadcast your Activity might be inactive due to orientation change or event that pauses your app so you might miss the event. i don't listen to system events but to my own events so i use sticky broadcast to prevent that issue.

提交回复
热议问题