1. Introduction of broadcast senders and broadcast receivers
1. Broadcast Receiver
The broadcast receiver is simply a Java class that receives the broadcast intent. This Java class inherits the broadcastreceiver class and is rewritten as follows:
Public void onreceive (context, intent), where intent can obtain transmitted data;
The broadcast intent is to use context. sendbroadcast (intent) or context. the intent sent by sendorderedbroadcast (intent). This statement can be used to broadcast to all components that meet the conditions. For example, intent sets action = "com. xiazdong ", then all in androidmanifest. <action Android: Name = "com. the broadcast recipients of xiazdong "/> can receive the broadcast;
Note: The onreceive method must be completed within 10 seconds. If it is not completed, "Application No response" will be thrown"When the onreceive method of the broadcast receiver needs to be executed for a long time, it is best to send the time-consuming work to the service through intent,Completed by serviceAnd cannot be solved using the subthread. Because broadcastreceiver is created after receiving the broadcast and has a short life cycle, the subthread may have been killed before execution.
public void onReceive(Context context,Intent intent){Intent intent = new Intent(context,XxxService.class);context.startService(intent);}
2. Broadcast senders
Generally, the Broadcast Sender calls the context. sendbroadcast () Program, and the broadcast receiver inherits the broadcastreceiver program;
Generally, broadcast senders send messages to multiple users through implicit intent;
Broadcast senders are divided into normal broadcast and ordered broadcast;
Synchronous Broadcast: After the sender sends a message, it almost arrives at multiple broadcast recipients at the same time. A receiver cannot receive the broadcast and then process it to the next receiver, and cannot terminate the broadcast. context. sendbroadcast (intent );
Ordered Broadcast: The broadcast receiver needs to set priority in advance. The broadcast is received first when the priority is high. The priority value is-1000 ~ 1000, in androidmanifest. XML <intent-filter Android: Priority = "XXX"> Settings. For example, three broadcast receivers A, B, and C have priority A> B> C, therefore, a receives the broadcast first. After receiving the broadcast, A can add some data to the broadcast to the next receiver (intent. putextra (), or terminate broadcast (abortbroadcast (); context. sendorderedbroadcast (intent );
Ii. Core broadcast receiver code
Core code of synchronous Broadcast Sender:
Intent intent = new Intent();intent.setAction("...");Context.sendBroadcast(intent);
Core code of ordered broadcast senders:
Intent intent = new Intent();intent.setAction("...");Context.sendOrderedBroadcast(intent,null);
Core broadcast receiver code:
public class Receiver extends BroadcastReceiver{public void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();...}}
Androidmanifest. xml
<application> <receiver android:name=".Receiver"> <intent-filter android:priority="1000"> <action android:name="com.xiazdong"/></intent-filter></receiver></application>
Iii. Broadcast instances
1. synchronous broadcast instance
Scenario Description:
(1) broadcast senders:
Package COM. xiazdong. broadcastsender; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class mainactivity extends activity {private button; private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {intent = new intent (); intent. setaction ("com. xiazdong "); intent. putextra ("name", "xiazdong"); mainactivity. this. sendbroadcast (intent); toast. maketext (getapplicationcontext (), "broadcast sent successfully", toast. length_short ). show () ;};@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) This. findviewbyid (R. id. button); button. setonclicklistener (listener );}}
(2) broadcast Receiver
Package COM. xiazdong. broadcastreceiver1; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. util. log; public class extends er extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {string name = intent. getextras (). getstring ("name"); log. I ("recevier1", "Received:" + name );}}
Androidmanifest. xml
<receiver android:name=".Receiver"> <intent-filter> <action android:name="com.xiazdong"/> </intent-filter></receiver>
Result:
2. Ordered broadcast instance
Scenario Description:
(1) Broadcast Sender
Package COM. xiazdong. broadcastsender; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class mainactivity extends activity {private button; private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {intent = new intent (); intent. setaction ("com. xiazdong "); intent. putextra ("name", "xiazdong"); mainactivity. this. sendorderedbroadcast (intent, null); // send toast for ordered broadcast. maketext (getapplicationcontext (), "broadcast sent successfully", toast. length_short ). show () ;};@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) This. findviewbyid (R. id. button); button. setonclicklistener (listener );}}
(2) broadcast Receiver
Receiver1
Package COM. xiazdong. broadcastreceiver1; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. util. log; public class extends er extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {string name = intent. getextras (). getstring ("name"); log. I ("recevier1", "Received:" + name); abortbroadcast (); // receiver1 interrupted broadcast after receiving }}
Androidmanifest. xml
<Cycler Android: Name = ". Cycler"> <intent-filter Android: Priority = "1000"> <! -- Set the highest priority --> <action Android: Name = "com. xiazdong"/> </intent-filter> </receiver>