Android: Check if activity is destroyed by a system from service

后端 未结 1 1089
南旧
南旧 2021-01-22 01:37

I have a service listening to some events from server. A service has START_STICKY flag that makes him restart when it\'s killed by OS. When service receive an event i have two s

相关标签:
1条回答
  • 2021-01-22 01:58

    It sounds like you are using LocalBroadcastManager. That's good. Its sendBroadcast() method returns a boolean indicating if a registered receiver was found. You can use that result to determine if your receiving activity (MainActivity) exists and has registered to receive the broadcast.

    When your service has an event to send to MainActivity, first try to send the event using sendBroadcast(). If it returns true, your done. If it returns false, the activity is not registered and must be created using startActivity(), with the event passed as an extra, as shown in your posted code.

    0 讨论(0)
提交回复
热议问题