Summary BroadcastReceiver Android components of the system

1, common classification
    BroadCastReceiver, press registration can be divided into static and dynamic broadcast radio receivers receiver.
    Static Radio Receiver: whether to start the program without restraint, when the application is closed, or can be received (life cycle is generally broadcast receiver is currently active and life-cycle synchronized) broadcasts.
    Dynamic broadcast receivers: free to control the registration and cancellation, there is a lot of flexibility. But only to receive broadcast after the program starts.

    For the broadcast (Broadcast), the broadcast can be divided into ordinary and orderly broadcast.
    Wherein general broadcast care sequence, the processes of the broadcast receiver can receive the broadcast substantially simultaneously. And orderly broadcast system according to a priority order, one of the recipient declaration reception process, first the receiver receives a broadcast order, may be modified or truncated to the orderly broadcasting.
    Static registration of a broadcast receiver, when receiving a broadcast, the system automatically broadcast an orderly way to serial processing (because the process can not create concurrent), such a receiver receives a broadcast of the order, and the receiver is connected package name-related, or that the scanning order and PMS related.
    Dynamic registration of radio receiver, if receiving ordinary broadcast receiver received the order of the broadcast and related registration order. In all common broadcast inside dynamic registration of a broadcast receiver, with respect to the registration of a static broadcast receivers, it will give priority to receive normal broadcast.

    With dynamic priority order broadcast, the order of registration affect the reception of broadcast order; the impact with the static priority order broadcast, the scan order to receive the order.
In addition to these, there is a relatively less common mentioned earlier: the broadcast receiver application LocalBroadcastManager Register can only be registered by the dynamic LocalBroadcastManager.


2, the registration process outlined
    dynamically registered broadcast receiver, primarily through ContextImpl, LoadedApk, then call a method of AMS registerReceiver completed.
Static registration broadcast receivers, when the system is started, the PMS resolve apk file and record receivers of information, then call the AMS PMS interface to query intent matching information, and then complete the registration process broadcast.

3, transmission and reception procedure outlined
    transmits broadcasting, mainly through ContextImpl, LoadedApk, AMS, ActivityThread completed. Upon receiving the broadcast, the first inside AMS process, then added to a queue to find the recipient, again corresponding thread sends a broadcast message.

    Starting Android O, static registration system for broadcast receivers add a restriction, you must specify where the broadcast receiver can send the package name, or use Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND this flag, but the flag is hidden, if you must use a static broadcast reception is, can not specify a package name (e.g., a broadcast transmission to-many), it is possible to use specific numerical values ​​of the flag 0x01000000.


4, security
    can be used to enhance the security of broadcast rights received. If the receiver is defined rights, broadcast transmission needs permission to send a corresponding statement; on the contrary, as otherwise not receive. On the whole, the following situations:

  • Specify the sender, the sender has permissions to send this broadcast;
  • A designated recipient, the recipient can receive the broadcast rights statement;
  • Specify the sender and the recipient (rarely used).

    In either case, it is related to permission custom permissions, so need corresponding statement in AndroidManifest.xml.
    For the first case (receiver defined permissions to restrict the sender), if it is a dynamic broadcast receiver, which can be passed at the registration permission by registerReceiver. Static broadcast receiver is in AndroidManifest.xml inside, where the definition broadcast receivers, add android: permission attribute, the attribute to customized permissions.


5, onReceive Context () into parameters in
    this context is a variable parameter of the particular type of examples, can be divided into the following situations.
    1), the static registration broadcast receiver
    in this case, the reference is android.app.ReceiverRestrictedContext type, can not be used to start Activity, pop AlertDialog (except Dialog system and application type is SYSTEM_ALERT_WINDOW type).

    2), the dynamic broadcast receiver registered
    DETAILED was divided into two cases:
    A) Activity in the register inside the broadcast receiver. At this time, the parameters of the context Activty onReceive objects is registered broadcast receiver.
At this time, since the reference to the Context object contex Activity can be used to start Activity, pop AlertDialog.
However, considering the onReceive () method in the main thread, the process needs to be performed is finished within 10 seconds, a short lifetime. If the dialog box need to wait for a user response, we need to consider the dialog management issues. Common practice is to place it inside the Service management, registering a dynamic broadcast receivers when Service starts, the cancellation of the Service stops.
    b) Service register in which the broadcast receiver. At this point onReceive into the context of the reference should the Service object, irrespective of whether the Service and across processes. Context in this case can not start Activity, has several limitations.
In both cases can be unified, onReceive of the reference context is the calling component registerReceiver of Context.

    There are other LocalBroadcastManager registration applications within broadcast receiver. Its onReceive in the context of the Application Context.

(Complete and relevant articles into the system, can be found in my original e-book open source "Android system performance and optimization", address: https: //github.com/carylake/androidnotes)

Guess you like

Origin www.cnblogs.com/carylake/p/12018933.html