There are too many broadcasts in Android, just a little summary today.
They are divided into two categories by way of registration:
1. Static Registration Broadcast:
Static registration broadcast is to register the broadcast in the Androidmanifest.xml file, suppose we want to achieve such an effect, click a button on an activity, send a broadcast, this broadcast pops up a toast, display "static" word.
First look at the broadcast recipient:
public class Mybroadcast extends Broadcastreceiver {
@Override
the public void onreceive (context context, Intent Intent) {
Toast.maketext (context, "static", Toast.length_long). Show ();
}
Register in manifest file:
<receiver android:name= "Com.example.staticbroadcast.MyBroadcast" >
<intent-filter>
< Action android:name= "Com.test.StaticBroadcast"/>
</intent-filter>
</receiver>
Click events in activity (send broadcast):
This.static_btn.setOnClickListener (New Onclicklistener () {
@Override public
void OnClick (View v) {
Intent Intent = new Intent ();
Intent.setaction ("Com.test.StaticBroadcast");
Sendbroadcast (intent);
}
);
2. Dynamic registration:
Dynamic registration is typically registered in the OnStart () method in the activity, and is onstop in the () method, following the code:
public class Mainactivity extends activity {private Button static_btn;
Private Button dynamic_btn;
Private Broadcastreceiver Mybroadcastreceiver;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
THIS.STATIC_BTN = (Button) This.findviewbyid (R.id.button1);
THIS.DYNAMIC_BTN = (Button) This.findviewbyid (R.ID.BUTTON01); Mybroadcastreceiver = new Broadcastreceiver () {@Override public void onreceive (context context, Intent Intent {Toast.maketext (mainactivity.this, "Hello, this is a dynamic broadcast!")
", Toast.length_long). Show ();
}
}; This.static_btn.setOnClickListener (New Onclicklistener () {////@Override//public void OnClick (View v) {/
/Intent Intent = new Intent ();
Intent.setaction ("Com.test.StaticBroadcast");
Sendbroadcast (Intent);
// }
// });
This.dynamic_btn.setOnClickListener (New Onclicklistener () { @Override public void OnClick (View v) {//send broadcast Intent Intent = new Intent ();
Intent.setaction ("Dynamicbroadcast");
Sendbroadcast (Intent);
}
});
} @Override protected void OnStart () {Super.onstart ();
Registered broadcast Intentfilter Intentfilter = new Intentfilter ();
Intentfilter.addaction ("Dynamicbroadcast");
Registerreceiver (Mybroadcastreceiver, Intentfilter);
} @Override protected void OnStop () {super.onstop ();
Cancellation of registration Unregisterreceiver (mybroadcastreceiver);
}
}
Details about static registration:
android:exported= The property "True" indicates whether the broadcast receiver receives broadcasts from other apps, and if there are intent-filter properties, the default is true, otherwise the default is False.
Each broadcast receiver can accept multiple broadcast sources, and if it is statically registered, then you have to do this:
<receiver
android:exported= "true"
android:name= "Com.example.staticbroadcast.MyBroadcast" >
<intent-filter>
<action android:name= "Com.test.StaticBroadcast"/>
<action android:name= " Com.test.StaticBroadcast2 "/>
</intent-filter>
</receiver>
This is handled in the broadcast receiver:
@Override public
void OnReceive (context context, Intent Intent) {
if (intent.getaction (). Equals (" Com.test.StaticBroadcast ")) {
Toast.maketext (context," static ", Toast.length_short). Show ();
} else if ( Intent.getaction (). Equals ("Com.test.StaticBroadcast2")) {
Toast.maketext (context, static 2, Toast.length_short). Show ();
}
If the registration is dynamic, the registration method is as follows:
@Override
protected void OnStart () {
super.onstart ();
Registered broadcast
Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction ("Dynamicbroadcast");
Intentfilter.addaction ("DynamicBroadcast2");
Registerreceiver (Mybroadcastreceiver, intentfilter);
}
The broadcast receiver is handled in the same way as static registration.
about how to use broadcast to implement communication between activity and fragment you can see my other blog using broadcast to implement communication between Android components
Original address: http://blog.csdn.net/u012702547/article/details/46955787
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.