Broadcast

Source: Internet
Author: User
Normal broadcast


Create a broadcast receiver

Class broadcast extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){

Log. I ("Ma", "broadcast ");

}}

Create a broadcast transmitter

Intent intent = new Intent ();

Intent. setAction ("com. xiazdong ");

SendBroadcast (intent );


Create a filter to control the content to be received

IntentFilter filter = new IntentFilter ();
Filter. addAction ("com. xiazdong ");

Open Broadcast Receiver
RegisterReceiver (new broadcast, filter );


Linear broadcast

Create two broadcast receivers

Class broadcast01 extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){

Log. I ("Ma", "first broadcast ");

AbortBroadcast ();

}}

Class broadcast02 extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){

Log. I ("Ma", "second broadcast ");

}}




Register two broadcast receivers in AndroidManifest. xml <receiver er android: name = ". broadcast01">
<Intent-filter android: priority = "20"> <! -- Control the priority level. The priority value is-1000--1000, and 1000 is the highest priority. --->
<Action android: name = "com. example. broadcast. broadcast"/>
               
</Intent-filter>
</Cycler>
<Cycler android: name = ". broadcast02">
<Intent-filter android: priority = "20">
<Action android: name = "com. example. broadcast. broadcast"/>
</Intent-filter>
</Cycler>


Create a broadcast transmitter

Intent intent = new Intent ();

Intent. setAction ("com. example. broadcast. broadcast ");

SendOrderedBroadcast (intent, null );


Sticky broadcast

Create a broadcast receiver

Class broadcast extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){

Log. I ("Ma", "broadcast ");

}}

Create a broadcast transmitter

Intent intent = new Intent ();

Intent. setAction ("com. xiazdong ");

SendStickyBroadcast (intent );


Create a filter to control the content to be received

IntentFilter filter = new IntentFilter ();
Filter. addAction ("com. xiazdong ");

Open Broadcast Receiver
RegisterReceiver (new broadcast, filter );


<! -- To send a sticky message, you must obtain the permission in the configuration file: -->

<Uses-permission android: name = "android. permission. BROADCAST_STICKY"/>


Broadcast

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.