One, broadcast 2 different ways of registering the choice:
Andorid broadcasts have manual registration and manifest file registration in 2 ways:
- Manifest file registration, no need to do anything in the code, or even instantiate the broadcast receiver. Because Android will create an instance and manage its life cycle. Note that the instance that receives the OnReceive () call is valid only for the duration of OnReceive (). In fact, Each call to the OnReceive () method is likely to occur on a different broadcast receive instance. This means that you must avoid calling other classes asynchronously.
- Manual registration, you must log off the broadcast when not in use, and if the logoff failure in an activity causes an exception, Andorid will issue a warning of a content leak. This is one of the main differences between 2 different registrations, when manually registering, the application is responsible for the lifetime of the broadcast receiver, while the manifest file is registered, Android handles the life cycle.
Second, broadcast versus service comparison: Broadcast receivers are a lightweight application component that passively collects data compared to services. A broadcast receiver based on manifest registration does not need to exist outside of OnReceive (). So it can be garbage collected immediately after the method returns. The disadvantage of a broadcast receiver is that it does not maintain state between individual onreceive (). Once the OnReceive () method returns, the instance is garbage collected. May cause the instance data to not be saved, thus affecting the next onreceive () call, may be an insurmountable problem,