Broadcast Receiver Register in Manifest vs. Activity

If your receiver is registered in the manifest, and your app is not running, a new process will be created to handle the broadcast. If you register it in code, it’s tied to the life of the activity/service you registered it in. For some broadcasts, it doesn’t really make sense to create a new app process if it doesn’t exist, or there are some security, performance, etc. implications, and thus you can only register the receiver in code.

As for the HEADSET_PLUG broadcast, it seems the idea is that your already running app can get this to do app-specific adjustments to UI, volume, etc. If your app is not running, you shouldn’t really care about the headphones being unplugged.

AFAIK, there is no single place this info is summarized for all broadcasts, but each Intent should have a comment in the JavaDoc about how to register and use it, but apparently it’s lacking in places. You should be able to compile a list if you grep the Android source tree for Intent.FLAG_RECEIVER_REGISTERED_ONLY.

Leave a Comment