1. Broadcast type: standard broadcast and ordered broadcast . Standard broadcasts are asynchronous broadcasts that are received almost at the same time by all receivers after broadcast, without sequencing, high efficiency, but cannot be truncated. An ordered broadcast is a synchronous broadcast that only one receiver receives this message at the same time, and then continues to pass after execution, and the priority receiver can accept it and truncate the broadcast pass.
Dynamic Registration Case Study: Network change alertBuild Response Class: Class Networkchangereciver extendsBroadcastreceiver{
@Override
public void OnReceive (Context context,intent Intent) {
Connectivitymanager connectivitymanager= (Connectivitymanager) Getsystemservice (Context.connectivity_service);
Networkinfo Networkinfo=connectivitymanager.getactivenetworkinfo ();
if ((Networkinfo!=null&&networkinfo.isavailable ()))
{
Toast.maketext (Context, "Network available", Toast.length_short). Show ();
}
Else
{
Toast.maketext (Context, "Network unavailable", Toast.length_short). Show ();
}
A response is registered when the activity is created, and the classification filters the message private Intentfilter intentfilter;//filters the message
Private Networkchangereciver Networkchangereciver;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Intentfilter=new Intentfilter ();
Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE");
Networkchangereciver=new Networkchangereciver ();
Registerreceiver (Networkchangereciver,intentfilter);} Destroy Listener @Override
protected void OnDestroy ()
{
Super.ondestroy ();
Unregisterreceiver (Networkchangereciver);
Finally, add the permissions in the configuration file <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>Static registration Case studies (The benefit is that you can listen to the broadcast without starting the program)Boot upDefine a listener class public class Bootcompletereceiver extends Broadcastreceiver {
@Override
public void OnReceive (Context context,intent Intent)
{
Toast.maketext (Context, "Start complete", Toast.length_short). Show ();
}
Modify Android Profile <?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.zhb.test2" >
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/><application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<receiver android:name= "Com.example.zhb.Com.BootCompleteReceiver" >
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver><activity
Android:name= ". Mainactivity "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest> 2. Sending standard broadcasts and ordered broadcastsStandard Broadcast:define a Receive broadcast classpublic class Mybroadcastreceiver extends Broadcastreceiver {
@Override
public void OnReceive (Context context,intent Intent)
{
String Value=intent.getstringextra ("key");
Toast.maketext (Context,value,toast.length_short). Show ();
}
}Configuration profile Information<receiver android:name= "Com.example.zhb.Com.MyBroadcastReceiver" >
<intent-filter>
<action android:name= "Com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>send a message with dataButton Btnbrctestone = (button) Findviewbyid (R.id.btnbrctestone);
Btnbrctestone.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent ("Com.example.broadcasttest.MY_BROADCAST");
Intent.putextra ("Key", "I am the Broadcast news!") ");
Sendbroadcast (Intent);
}
}ordered Messages: Just change one line of code @Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.brocasttest);
Button Btnbrctestone = (button) Findviewbyid (R.id.btnbrctestone);
Btnbrctestone.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent ("Com.example.broadcasttest.MY_BROADCAST");
Intent.putextra ("Key", "I am the Broadcast news!") ");
Sendbroadcast (Intent);
Sendorderedbroadcast (intent,null);
}
}
);
}Set Priority Receive permissions<receiver android:name= "Com.example.zhb.Com.MyBroadcastReceiver" >
<intent-filterandroid:priority= ">
<action android:name= "Com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver> truncate broadcast continues to propagate public class Mybroadcastreceiver extends Broadcastreceiver {
@Override
public void OnReceive (Context context,intent Intent)
{
String Value=intent.getstringextra ("key");
Toast.maketext (Context,value,toast.length_short). Show ();
abortbroadcast ();
}
Local broadcast: All previous broadcasts are global broadcasts, that is, broadcasts that are issued can be received by any program, and can also accept broadcasts to any program. This can easily lead to security, such as data being monitored by other programs, or other programs that are constantly sending spam broadcasts. Therefore, there is a local broadcasting mechanism. Broadcasts can only be delivered within the application, and receivers can only receive broadcasts from the application. Local broadcasts cannot be registered in a configuration file by means of a static method. public class Brocasttest extends Activity {
PrivateIntentfilterFilter for intentfilter;//frame
PrivateLocalreceiverlocalreceiver;//a custom receiver
PrivateLocalbroadcastmanagerlocalbroadcastmanager;//Framework's local broadcast manager
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.brocasttest);
Localbroadcastmanager=localbroadcastmanager.getinstance (this);//Get instance
Button Btnbrctestone = (button) Findviewbyid (R.id.btnbrctestone);
Btnbrctestone.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Send a local broadcast
Intent intent1=new Intent ("Com.example.broadcasttest.LOCAL_BROADCAST");
Localbroadcastmanager.sendbroadcast (intent1);
}
}
);
intentfilter=new Intentfilter ();
Intentfilter.addaction ("Com.example.broadcasttest.LOCAL_BROADCAST");
Localreceiver=new Localreceiver ();
Localbroadcastmanager.registerreceiver (localreceiver,intentfilter);
}
@Override
protected void OnDestroy ()
{
Super.ondestroy ();
Localbroadcastmanager.unregisterreceiver (localreceiver);
}
Class Localreceiver extendsBroadcastreceiver
{
@Override
public void OnReceive (Context context,intent Intent)
{
Toast.maketext (Context, "received local broadcast", Toast.length_short). Show ();
}
}
}
Android Beginner to Skilled (v)---broadcast