How to use Intents from a Service or Broadcast Receiver?

后端 未结 1 1901
悲哀的现实
悲哀的现实 2021-01-14 22:47

I need to be able to handle/catch Intents while my Activity is closed. So I am looking at either a Service or a BroadcastReceiver.

Is it possible to \"receive\" i

1条回答
  •  孤城傲影
    2021-01-14 23:31

    Broadcast Receivers are application components that can run independent of Activities and Services, so the use-case you describe is definitely supported.

    If you register an intent-filter tag for the broadcast Intents you want to handle in the receiver manifest node, it will receive all the matching broadcast Intents even if your application process is completely dead (no Activities or Services).

    The following snippet shows how you would add a Broadcast Receiver to your manifest to listen for a broadcast Intent, independent of an Activity or Service running.

    
      
        
      
    
    

    Within your Broadcast Receiver implementation, the onReceive handler will be called when the Intent action for which you specified an intent-filter is broadcast.

    Note: There are certain system broadcast's that you can't capture this way, but generally speaking this is the approach to take for responding to system events when you have no Activity running.

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