First, broadcast Sender & Broadcast receiver Introduction
1. Broadcast Receivers
The broadcast receiver is simply the Java class that receives the broadcast intent, and this Java class inherits the Broadcastreceiver class and overrides:
public void OnReceive (context context,intent Intent), in which Intent can get the data passed;
The purpose of the broadcast is to pass the intent of the Context.sendbroadcast (Intent Intent) or Context.sendorderedbroadcast (Intent Intent) through this statement, To be able to broadcast to all components that meet the criteria, such as intent set action= "Com.xiazdong", all <action android:name= "set in Androidmanifest.xml" Com.xiazdong "/> Broadcast receivers are able to receive broadcasts;
Note: The OnReceive method must be completed in 10 seconds, if not completed, then cast "Application No Response" when the broadcast receiver OnReceive method takes a long time to execute, it is best to send this time-consuming work through intent to the service, Completed by the service and cannot be resolved using a child thread, because the Broadcastreceiver is created after the broadcast is received, and the lifecycle is short, so the child thread may have been killed without execution.
public void OnReceive (context context,intent Intent) {
Intent Intent = new Intent (context,xxxservice.class);
Context.startservice (Intent);
2. Broadcast Sender
Usually the broadcast sender is the program that invokes Context.sendbroadcast (), and the broadcast receiver is the program that inherits the Broadcastreceiver;
Usually the sender of the broadcast is implicitly intent so that it can be sent to many people;
The broadcast sender is divided into ordinary broadcast and orderly broadcast;
Synchronous broadcast: After the sender is issued, almost at the same time to reach multiple broadcast receivers, a receiver can not receive the broadcast after processing to the next receiver, and can not terminate the broadcast continue to spread; Context.sendbroadcast (intent);
Ordered broadcast: Broadcast receivers need to set priority in advance, high priority to receive broadcast first, priority series value of -1000~1000, in androidmanifest.xml <intent-filter android:priority= "xxx" > Settings, for example, there are 3 broadcast receivers A, B, C, priority A>b>c, so a first received the broadcast, when a received broadcast, can add some data to the broadcast to the next receiver (Intent.putextra ()), or terminate the broadcast ( Abortbroadcast ()); Context.sendorderedbroadcast (intent);
Second, broadcast receiver core code
Synchronous broadcast Sender Core code:
Intent Intent = new Intent ();
Intent.setaction ("...");
Ordered broadcast Sender core code:
Intent Intent = new Intent ();
Intent.setaction ("...");
Context.sendorderedbroadcast (Intent,null);
Broadcast receiver Core Code:
public class Receiver extends broadcastreceiver{public
void OnReceive (context context, Intent Intent) {
Bundle b Undle = Intent.getextras ();
...
}
Androidmanifest.xml
<application>
<receiver android:name= ". Receiver ">
<intent-filter android:priority=" 1000 ">
<action android:name=" Com.xiazdong "/ >
</intent-filter>
</receiver>
</application>
Iii. Broadcast Examples
1. Synchronous Broadcast Instance
Scenario Description:
(1) Broadcast sender:
package com.xiazdong.broadcastsender;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends activity {private button button; Private Onclicklistener listener = new Onclicklistener () {@Override public void OnClick (View v) {Intent
Intent = new Intent ();
Intent.setaction ("Com.xiazdong");
Intent.putextra ("name", "Xiazdong");
MainActivity.this.sendBroadcast (Intent);
Toast.maketext (Getapplicationcontext (), "Send broadcast Success", Toast.length_short). Show ();
}
};
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button = (button) This.findviewbyid (R.id.button);
Button.setonclicklistener (listener); }
}
(2) Broadcast receivers
Package com.xiazdong.broadcastreceiver1;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.util.Log;
public class Receiver extends Broadcastreceiver {
@Override
the public void onreceive (context context, Intent Intent {
String name = Intent.getextras (). getString ("name");
LOG.I ("Recevier1", "Received:" +name);
}
Androidmanifest.xml
<receiver android:name= ". Receiver ">
<intent-filter>
<action android:name=" Com.xiazdong "/>
</ Intent-filter>
Results:
2. Ordered broadcast instance
Scenario Description:
(1) Broadcast sender
Package com.xiazdong.broadcastsender;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends activity {private button button; Private Onclicklistener listener = new Onclicklistener () {@Override public void OnClick (View v) {Intent
Intent = new Intent ();
Intent.setaction ("Com.xiazdong");
Intent.putextra ("name", "Xiazdong"); MainActivity.this.sendOrderedBroadcast (intent, NULL);
Ordered broadcast send Toast.maketext (Getapplicationcontext (), "Send broadcast Success", Toast.length_short). Show ();
}
};
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button = (button) This.findviewbyid (R.id.button);
Button.setonclicklistener (listener);
}
}
(2) Broadcast receivers
Receiver1
Package com.xiazdong.broadcastreceiver1;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.util.Log;
public class Receiver extends Broadcastreceiver {
@Override
the public void onreceive (context context, Intent Intent {
String name = Intent.getextras (). getString ("name");
LOG.I ("Recevier1", "Received:" +name);
Abortbroadcast (); Receiver1 interrupt broadcast after receiving broadcast
}
}
Androidmanifest.xml
<receiver android:name= ". Receiver ">
<intent-filter android:priority=" 1000 "> <!--Set highest priority-->
<action android: Name= "Com.xiazdong"/>
</intent-filter>
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.