1. Static registration.
Register in the manifest.
1 < android:name= "Com.exce.learnbroadcastreceiver.MyReceiver"></ Receiver>
Then use the Sendbroadcast (Intent) method in the activity. Where the parameter is intent.
1 Intent intent=new Intent (This, myreceiver. Class); 2 Intent.putextra ("Data", "Exer"); Carrying data 3 sendbroadcast (intent);
2. Dynamic registration.
You do not need to register in manifest.
Use Registerreceiver () and Unregisterreceiver () for broadcast registration and de-registration in your code.
Registerreceiver (broadcastreceiver receiver, Intentfilter filter), the first parameter is receiver, the second parameter is Intentfilter.
Unregisterreceiver (Broadcastreceiver Receiver), the parameter is receiver.
Code:
1 Private Myreceiver receiver;
...
1 CaseR.id.regbroadcast:2 if(receiver==NULL) {3Receiver=Newmyreceiver ();4Registerreceiver (receiver,NewIntentfilter (myreceiver.action));5 }6 Break;7 CaseR.id.unregbroadcast:8 if(receiver!=NULL) {9 Unregisterreceiver (receiver);TenReceiver=NULL; One}
Myreceiver:
1 Public Static Final String action= "Com.exec.learnbroadcastreceiver.intent.action";
Intentfilter is obtained by constant action,new Intentfilter (myreceiver.action). Then you can register for the broadcast.
Dynamic registration of the broadcast, must be destroyed at the time of the cancellation of registration. That is, Registerreceiver () and Unregisterreceiver () must appear at the same time.
Priority of 3.Broadcast
The Broadcastreceiver priority can be obtained through the android:priority= "" property of Intent-filter in manifest. Android:priority= "9" need to fill in a number, the larger the number, the higher the priority.
for an ordered broadcast , you can use the Sendorderedbroadcast (Intent Intent, String receiverpermission) method, and then mate with the android:priority= "" Property, Make an orderly broadcast
If you want to truncate the broadcast , you can truncate the broadcast by using the Abortbroadcast () method in the OnReceive () method of the Myreceiver that inherits Broadcastreceiver.
The use of Android development Broadcastreceiver