The type and use of the Android check trap--broadcastreceiver

Source: Internet
Author: User

Broadcast is a mechanism that is used to transfer information between applications and applications. A broadcast can correspond to multiple recipients. A complete broadcast mechanism that needs to have the following three elements:

    • Send a broadcast broadcast
    • Accept the broadcastreceiver of the broadcast
    • Intent of transmitting information

The registration of broadcasts is divided into static registration and dynamic registration:

    • Static registration: A statically registered broadcast is a broadcast that is registered in the Androidmanifest, which is parsed by the system when the application is installed and can receive the corresponding broadcast without having to launch the application.
<receiver android:name=".broadcast.MyBroadcastReceiver">    <intent-filter>        <action android:name="MY_BROADCAST_RECEIVER" />    </intent-filter></receiver>
    • Dynamic registration: Implemented by Context.registerreceiver (), when it is not necessary to de-broadcast via Context.unregisterreceiver (), this type of broadcast must be applied before it can register and receive broadcasts.

      // 动态注册广播接收器registerReceiver(new DynamicBroadcastReceiver(), new IntentFilter(MyBroadcastReceiver.ACTION));

Broadcasting is divided into general broadcasting, orderly broadcasting, local broadcasting and sticky broadcasting.

I. GENERAL Broadcasting

Ordinary broadcasts are sent through Context.sendbroadcast (), and we have no way of setting the order in which receivers receive their regular broadcasts. In theory, the order in which all receivers receive broadcasts is indeterminate, but generally in the order in which they are registered in the Androidmainfest.xml file (not absolute).
In a normal broadcast, the recipient cannot pass the processing result to the next sink, nor can it terminate the propagation of the broadcast.

The following code is a statically registered broadcast example:

public class MyBroadcastReceiver extends BroadcastReceiver {    String TAG = MyBroadcastReceiver.class.getSimpleName();    public static final String ACTION = "MY_BROADCAST_RECEIVER";    @Override    public void onReceive(Context context, Intent intent) {        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));    }}

Then register this broadcast in Androidmainfest.xml:

<receiver android:name=".broadcast.MyBroadcastReceiver">    <intent-filter>        <action android:name="MY_BROADCAST_RECEIVER" />    </intent-filter></receiver>

Next, call Context.sendbroadcast () in the activity to send the broadcast:

Intent intent = new Intent(MyBroadcastReceiver.ACTION);intent.putExtra(INTENT_INFO, "我是一个普通广播");sendBroadcast(intent);

Log as follows:

12-08 17:29:44.259 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个普通广播
    • Receive sequence test for general broadcast
      We modeled mybroadcastreciver to create multiple receivers, like the code:
/** * 静态注册的广播接收器2 * Created by liuwei on 17/12/7. */public class MyBroadcast2Receiver extends BroadcastReceiver {    String TAG = MyBroadcast2Receiver.class.getSimpleName();    public static final String ACTION = "MY_BROADCAST_RECEIVER";    @Override    public void onReceive(Context context, Intent intent) {        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));    }}/** * 静态注册的广播接收器3 * Created by liuwei on 17/12/7. */public class MyBroadcast3Receiver extends BroadcastReceiver {...}/** * 静态注册的广播接收器4 * Created by liuwei on 17/12/7. */public class MyBroadcast4Receiver extends BroadcastReceiver {...}/** * 静态注册的广播接收器5 * Created by liuwei on 17/12/7. */public class MyBroadcast5Receiver extends BroadcastReceiver {...}/** * 静态注册的广播接收器6 * Created by liuwei on 17/12/7. */public class MyBroadcast6Receiver extends BroadcastReceiver {...}

Then register the same action for the above broadcast in the Androidmainfest.xml

<receiver android:name= ". Broadcast. Mybroadcastreceiver "> <intent-filter> <action android:name=" My_broadcast_receiver "/> </int Ent-filter></receiver><receiver android:name= ". Broadcast. Mybroadcast6receiver "> <intent-filter> <action android:name=" My_broadcast_receiver "/> </in Tent-filter></receiver><receiver android:name= ". Broadcast. Mybroadcast2receiver "> <intent-filter> <action android:name=" My_broadcast_receiver "/> </in Tent-filter></receiver><receiver android:name= ". Broadcast. Mybroadcast3receiver "> <intent-filter> <action android:name=" My_broadcast_receiver "/> </in Tent-filter></receiver><receiver android:name= ". Broadcast. Mybroadcast4receiver "> <intent-filter> <action android:name=" My_broadcast_receiver "/> </in Tent-filter></receiver><receiver android:name= ". Broadcast. MyBRoadcast5receiver "> <intent-filter> <action android:name=" My_broadcast_receiver "/> </inten T-filter></receiver>

Click Send broadcast to view log:

12-08 17:29:44.259 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个普通广播12-08 17:29:44.268 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个普通广播12-08 17:29:44.271 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个普通广播12-08 17:29:44.273 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast3Receiver: 接收到广播消息:我是一个普通广播12-08 17:29:44.277 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast4Receiver: 接收到广播消息:我是一个普通广播12-08 17:29:44.280 6644-6644/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: 接收到广播消息:我是一个普通广播
Second, orderly broadcasting

When a broadcast is registered in Androidmainfest.xml, priority is added to the broadcast by the node (the higher the value priority), and then the Context.sendorderedbroadcast () is sent, and the receivers are executed in order of priority.

The receiver of the ordered broadcast and the next recipient are passed the data, and the receiver can discard the broadcast after receiving the broadcast, so that the broadcast is no longer passed back.

Add a priority to the above 6 receivers:

<receiver android:name= ". Broadcast. Mybroadcastreceiver "> <intent-filter android:priority=" 1 "> <action android:name=" MY_BROADCAST_RECEIV ER "/> </intent-filter></receiver><receiver android:name=". Broadcast. Mybroadcast6receiver "> <intent-filter android:priority=" 6 "> <action android:name=" MY_BROADCAST_RECEI VER "/> </intent-filter></receiver><receiver android:name=". Broadcast. Mybroadcast2receiver "> <intent-filter android:priority=" 2 "> <action android:name=" MY_BROADCAST_RECEI VER "/> </intent-filter></receiver><receiver android:name=". Broadcast. Mybroadcast3receiver "> <intent-filter android:priority=" 3 "> <action android:name=" MY_BROADCAST_RECEI VER "/> </intent-filter></receiver><receiver android:name=". Broadcast. Mybroadcast4receiver "> <intent-filter android:priority=" 4 "> <action android:name=" MY_BROADcast_receiver "/> </intent-filter></receiver><receiver android:name=". Broadcast. Mybroadcast5receiver "> <intent-filter android:priority=" 5 "> <action android:name=" MY_BROADCAST_RECEI VER "/> </intent-filter></receiver>

Then send the broadcast through Sendorderedbroadcast to observe the log:

intent = new Intent(MyBroadcastReceiver.ACTION);intent.putExtra(INTENT_INFO, "我是一个有序广播");sendOrderedBroadcast(intent, null);

Log as follows:

12-08 18:17:26.455 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个有序广播12-08 18:17:26.462 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: 接收到广播消息:我是一个有序广播12-08 18:17:26.464 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast4Receiver: 接收到广播消息:我是一个有序广播12-08 18:17:26.465 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast3Receiver: 接收到广播消息:我是一个有序广播12-08 18:17:26.466 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个有序广播12-08 18:17:26.467 25919-25919/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个有序广播
    • Abortbroadcast () abandon the broadcast:
      There is no way to discard a normal broadcast, or it will throw a runtimeexception exception.

Only ordered broadcasts can be discarded by this method. We add the Abortbroadcast () method to the Mybroadcast6receiver:

public class MyBroadcast6Receiver extends BroadcastReceiver {    String TAG = MyBroadcast6Receiver.class.getSimpleName();    public static final String ACTION = "MY_BROADCAST_RECEIVER";    @Override    public void onReceive(Context context, Intent intent) {        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));        abortBroadcast();        Log.i(TAG, "丢弃广播");    }}

Then click Send ordered broadcast, log as follows:

12-08 18:34:27.989 1329-1329/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个有序广播12-08 18:34:27.989 1329-1329/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 丢弃广播

You can see that the broadcast has been discarded.

    • Setresult () is passed to the next recipient result.
    • GetResult () Receives the result of the previous recipient.

Add the Setresult method in Mybroadcast6receiver and add the GetResult method to the Mybroadcast5receiver:

public class MyBroadcast6Receiver extends BroadcastReceiver {    String TAG = MyBroadcast6Receiver.class.getSimpleName();    public static final String ACTION = "MY_BROADCAST_RECEIVER";    @Override    public void onReceive(Context context, Intent intent) {        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));//        abortBroadcast();//        Log.i(TAG, "丢弃广播");        setResult(006, "我是老6传来的消息", null);    }}public class MyBroadcast5Receiver extends BroadcastReceiver {    String TAG = MyBroadcast5Receiver.class.getSimpleName();    public static final String ACTION = "MY_BROADCAST_RECEIVER";    @Override    public void onReceive(Context context, Intent intent) {        Log.i(TAG, "接收到广播消息:" + intent.getStringExtra(BroadcastTestActivity.INTENT_INFO));        String data = getResultData();        Log.i(TAG, "data=" + data);    }}

Log as follows:

12-08 18:40:01.415 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast6Receiver: 接收到广播消息:我是一个有序广播12-08 18:40:01.434 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: 接收到广播消息:我是一个有序广播12-08 18:40:01.434 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast5Receiver: data=我是老6传来的消息12-08 18:40:01.440 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast4Receiver: 接收到广播消息:我是一个有序广播12-08 18:40:01.442 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast3Receiver: 接收到广播消息:我是一个有序广播12-08 18:40:01.445 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个有序广播12-08 18:40:01.447 10372-10372/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个有序广播
Third, Local broadcasting

The above broadcast for the system is global, after the broadcast, the system in the application as long as the corresponding receiver can be registered to receive the broadcast. If the broadcast that we want to send in this app can only be received within this app, then local broadcasts are available.

Local broadcast is managed by Localbroadcastmanager, it is added after API 21, it is convenient to use, it needs to get its singleton through the Localbroadcastmanager.getinstance () method First, The remainder of the usage is similar to other broadcasts, with the following main methods:

    • Registerreceiver (): Registers the broadcast receiver.
    • Unregisterreceiver (): Release the broadcast receiver.
    • Sendbroadcast (): Sends an asynchronous broadcast.
    • Sendbroadcastsync (): Sends a synchronous broadcast.

When using local broadcasts, you do not need to register in Androidmainfest.xml, you must use Localbroadcastmanager.getinstance (...). Registerreceiver (..) To register the receiver.

Let's write a local broadcast of the little chestnuts, first register two local broadcasts:

LocalBroadcastManager.getInstance(context).registerReceiver(new MyBroadcastReceiver(), new IntentFilter(MyBroadcastReceiver.ACTION));LocalBroadcastManager.getInstance(context).registerReceiver(new MyBroadcast2Receiver(), new IntentFilter(MyBroadcastReceiver.ACTION));

Then send the local broadcast:

intent.putExtra(INTENT_INFO, "我是一个本地广播");LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

Log as follows:

12-09 17:20:47.799 15019-15019/cn.codingblock.androidadvancestudy I/MyBroadcastReceiver: 接收到广播消息:我是一个本地广播12-09 17:20:47.799 15019-15019/cn.codingblock.androidadvancestudy I/MyBroadcast2Receiver: 接收到广播消息:我是一个本地广播
Iv. Sticky Broadcast (not recommended)

Sticky broadcast will remain in a state of detention, sticky broadcast is issued, as long as there is able to match its new receiver is registered to receive the broadcast, sticky broadcast through Context.sendstickybroadcast () sent.

Finally want to say is, this series for Bo master on Android knowledge again comb, check the learning process, on the one hand is forgetting things to review again, on the other hand believe in the process of re-learning will have a huge new harvest, if you also have with me the same idea, may wish to pay attention to my study together , explore each other and make progress together!

Reference documents:

    • Explore the art of Android development
    • "Android Development advanced from small to expert"

The type and use of the Android check trap--broadcastreceiver

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.