0

I'm going through the Wi-Fi Direct tutorials for Android and had a quick question about how they're using an IntentFilter in the section "Set Up a Broadcast Receiver and Peer-to-Peer Manager".

There, they manually configure an instance of IntentFilter for the activity and instantiate it in onCreate(). Then, in onResume(), the IntentFilter is passed in to registerReceiver().

What's the specific reason as to why the IntentFilter is configured here and what would be a reason as to why these intents couldn't just be set up in AndroidManifest.xml?

Alexander Trauzzi
  • 7,277
  • 13
  • 68
  • 112
  • 1
    Static broadcast receivers have their filters defined statically (in your manifest). Dynamic receivers have them defined in code. Not all receivers can or should be statically defined... for example, your application never needs to know _every single time_ the backlight comes on/off, but your application may need to know this _while it is running_. Statically defined, you learn of every such event even after you've stopped running. Dynamically, you disable when you don't need it anymore. – mah May 21 '13 at 10:43
  • 1
    mah answered it well. It's useful for intents that are received many times during your app lifetime. Another differences is that there are some intents that can not be recevied by a static receiver. For example, the screen on-off intent can be received only by dynamic receivers – edoardotognoni May 21 '13 at 11:06
  • Mah, feel free to answer the question for credit! :) – Alexander Trauzzi May 21 '13 at 11:13

0 Answers0