I. broadcastreceiver: used to receive broadcasts,
It has a callback method: void
Onreceive (context curcontext, intent broadcastmsg) is used to process the received broadcast.
Broadcast receiving: you can create a new class for receiving and inherit from broadcastreceiver. You can also create a broadcastreceiver instance, and then write the onreceive method to implement it as follows:
Protected broadcastreceiver mevtreceiver = new broadcastreceiver (){
@ Override
Public void onreceive (Context
Context, intent ){
String action = intent. getaction ();
// Judge by the registered action to process the Broadcast
If (action. Equals ("android. Intent. Action. boot_completed ")){
// Do something
}
}
};
Ii. Use of broadcastreceiver (two registration methods are available before use)
Method 1: manual registration and manual Cancellation
Registration:
Intentfilter filter = new intentfilter ();
Filter. addaction ("android. provider. telephony. sms_mongod ");
Registerreceiver (mevtreceiver, filter );
// Mevtreceiver indicates the broadcast name and filter indicates the broadcast action.
Logout: At last, the broadcast should be canceled in the ondestroy program. The implementation is as follows:
@ Override
Public void ondestroy (){
Super. ondestroy ();
Unregisterreceiver (mplayerevtreceiver );
}
Method 2: Register in manifast. xml
<Explorer Android: Name = ". mevtreceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
</Cycler>
3. You can also send broadcasts by yourself.
Intent intent = new intent ("android. Intent. Action. boot_completed ");
Sendbroadcast (I );
Then you receive the broadcast and read the first line.