Android Broadcast Mechanism
Method 1: static registration
Step 1:
Mybroadcastreceiver extends broadcastreceiver
Override the onreceive (context CT, intent) method to complete the work you want.
Step 2:
Register the Declaration in androidmanifest. xml:
<Cycler Android: Name = ". ringermodechangedreceiver">
<Intent-filter>
<Action Android: Name = "android. Media. ringer_mode_changed"/>
</Intent-filter>
</Cycler>
Method 2: Dynamic Registration
Write two methods in myactivity:
For example, register a broadcast receiver when myactivity is started and cancel the broadcast receiver when myactivity is destroyed.
Check the Code:
Public void oncreate (bundle ){
Registerreceiver (mybroadcastreceiver, new intentfilter ("custom or system string "));
}
Public void ondestory (){
Unregisterreceiver (mybroadcastrecevier );
}
(Static registration of broadcast is not allowed for some events, such as checking the remaining battery of the cell phone)