Reference blog:
http://blog.csdn.net/t12x3456/article/details/9256609
http://blog.csdn.net/lihenair/article/details/19343767
http://blog.csdn.net/plussoft/article/details/12971265
http://blog.csdn.net/xyz_fly/article/details/18970569
In Android, Broadcastreceiver is designed to be considered globally to facilitate communication between applications and systems, applications, and applications, so broadcastreceiver is a security issue for a single application. The corresponding problems and solutions are as follows:
1. When the application sends a broadcast, the system will match the sent intent with the intentfilter of all registered Broadcastreceiver in the system and execute the corresponding onreceive function if the match succeeds. An interface similar to Sendbroadcast (Intent, String) can be used to specify the permission that the receiver must have when sending broadcasts. or set a broadcast through Intent.setpackage is only valid for a program.
2. When an application registers for a broadcast, even if the intentfilter is set up, it will receive a broadcast from another application to match the decision. For dynamically registered broadcasts can be via interfaces like Registerreceiver (Broadcastreceiver, Intentfilter, String, Android.os.Handler) Specifies the permission that the sender must have, and for statically registered broadcasts, the android:exported= "false" property indicates that the recipient is unavailable to the external application, that is, the broadcast from outside is not accepted.
The above two problems can be solved by Localbroadcastmanager:
The Android V4 Compatibility Pack provides the Android.support.v4.content.LocalBroadcastManager tool class to help you send and register local broadcasts within your own process, using it more than directly through Sendbroadcast (Intent) send system global broadcast has the following advantages.
1 because broadcast data spreads within the scope of this application, you do not have to worry about the disclosure of privacy data.
2 Do not worry about other applications to forge broadcasts, causing security risks.
3 It is more efficient than sending a global broadcast within the system.
It is also used in a manner similar to a normal registered broadcast:
//use in Application Public Static voidsendlocalbroadcast (Intent Intent) {localbroadcastmanager.getinstance (getinstance ()). Sendbroadcastsync (Intent ); } Public Static voidRegisterlocalreceiver (broadcastreceiver receiver, Intentfilter filter) {localbroadcastmanager.getinstance (get Instance ()). Registerreceiver (receiver, filter); } Public Static voidUnregisterlocalreceiver (broadcastreceiver receiver) {localbroadcastmanager.getinstance (getinstance ()). Unregi Sterreceiver (receiver); }
Summary:
1, Localbroadcastmanager in the creation of a single case, do not have a tangled context is to take activity or application, it will take to Tapplicationcontext.
2. Localbroadcastmanager only works between code, because it is the object that holds the interface Broadcastreceiver, and then calls its OnReceive method directly.
3, Localbroadcastmanager registered after the broadcast, when the activity or fragment do not need to listen, remember to cancel registration, note: Registration and cancellation of registration in the activity or fragment the life cycle to maintain consistency, such as Onresume,onpause.
4, Localbroadcastmanager Although support for the same broadcastreceiver can register multiple intentfilter, but should be required to put the action in a intentfilter, Only one intentfilter is registered, this is my personal advice.
5. The broadcast action sent by Localbroadcastmanager can only interact with broadcastreceiver registered with Localbroadcastmanager. If you encounter a broadcast sent through Localbroadcastmanager, the opposite Broadcastreceiver is not responding, which is probably the cause.