Android broadcastreceiver
(Learning the video from instructor Mars)
Registration Method
There are two types:
One is declared in androidmanifest. xml:
|
<Cycler
Android: Name =". Test_receiver"
>
<Intent-filter>
<Action
Android: Name ="Android. Intent. Action. Sss"
/>
</Intent-filter>
</Cycler> |
Code in the class file:
|
Intent intent = NewIntent ();
Intent. setaction ("android. Intent. Action. Sss ");
Test_bcactivity.This. Sendbroadcast (intent ); |
The other is code registration:
Bind a consumer
|
PublicstaticfinalString Sms_received_action=
"Android. provider. telephony. sms_received ";
// Generate a receiver object
Extends ER = NewTest_receiver ();
//
Generate a filter object
Intentfilter = NewIntentfilter ();
//
Add an action for the filter
Intentfilter. addaction (Sms_received_action);
Test_bcactivity.This. Registerreceiver (receiver, intentfilter ); |
Unbind a consumer
|
Test_bcactivity.This. Unregisterreceiver (receiver ); |
The main difference between the two is that the broadcast mechanism registered in the XML file still runs after the application is closed. Always run. The broadcast registered by code is closed after the application is closed and takes effect immediately.
Code for the Cycler class
|
PublicclassTest_receiver ExtendsBroadcastreceiver {
PublicTest_receiver (){
Log.D("Test_receiver ",
"Constructor ");
}
@ Override
PublicvoidOnreceive (context, intent ){
// TodoAuto-generated method stub
Log.D("Test_receiver ",
"Onreceive ");
}
} |