Detailed explanation of the Android broadcast (Broadcastreceiver).
1. Broadcastreceiver Registration Process:
(1). Once the broadcast message is sent out, only the object that subscribes to the broadcast receives the broadcast message and makes the appropriate processing.
* * (2). **android broadcasts are divided into two areas: Broadcast senders and broadcast receivers. The broadcast in Android uses the Observer mode, a message based publish/subscribe event model. The broadcast receiver registers with the AMS through the binder mechanism, and AMS looks for the broadcastreceiver that meets the appropriate conditions and sends the broadcast to the corresponding message loop queue in Broadcastreceiver, which is typically an activity. Message loop execution gets this broadcast and recalls the OnReceive () method in Broadcastreceiver.
(3). The order of execution of the broadcast sender and the broadcast receiver is asynchronous, and the outgoing broadcast does not care about the reception of the receiver or the receiver.
2. Broadcastreceiver steps:
(1). Registered Broadcast: The broadcast receiver registers the broadcast with the AMS.
(2). Send a broadcast: A broadcast sent by a broadcaster to the AMS.
(3). Receive broadcast: The broadcast receiver receives the broadcast and invokes the OnReceive () method execution.
Here is an example of sending a text message with the following code:
Broadcastreceiverhelper class: Package com.scd.broadcastreceiver.helper;
Import com.scd.broadcastreceiver.activity.MainActivity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.widget.Toast; /** * Broadcast Receive * * @author SCD * */public class Broadcastreceiverhelper extends Broadcastreceiver {/** context/PRI
vate mainactivity mactivity = null;
Public Broadcastreceiverhelper (Mainactivity mcontext) {super ();
mactivity = Mcontext; @Override public void OnReceive (context context, Intent Intent) {if (Intent.getaction (). Equals (Mainactivity.ac
Tion_sendmessage)) {Toast ("sent successfully");
else if (intent.getaction (). Equals (Mainactivity.action_delivermessage)) {Toast ("receive success");
} public void Toast (String text) {Toast.maketext (mactivity, Text, Toast.length_short). Show (); }
}
Mainactivity class:
Package com.scd.broadcastreceiver.activity;
Import COM.SCD.BROADCASTRECEIVER.R;
Import Com.scd.broadcastreceiver.helper.BroadcastReceiverHelper;
Import android.app.Activity;
Import android.app.PendingIntent;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.telephony.gsm.SmsManager;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends activity implements Onclicklistener {private Button Mbutton = null;
/** Send SMS */public static String Action_sendmessage = "Com.scd.broadcastreceiver.MainActivity.SendMessage";
/** Receive SMS */public static String Action_delivermessage = "Com.scd.broadcastreceiver.MainActivity.DeliverMessage";
/** Reciver class * * Private broadcastreceiverhelper mhelpers = null; Private Broadcastreceiverhelper Mhelperd = nulL
/** SMS Management class * * Private Smsmanager Msmsmanager = null;
/** phone number/private String Mtelephone = "13607567010"; /** SMS content/private String Mcontext = "Hello, can I help you?"
";
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mbutton = (Button) Findviewbyid (r.id.sendbroadcast);
Mbutton.setonclicklistener (this);
Msmsmanager = Smsmanager.getdefault ();
Registered broadcast Registerbroadcast ();
@Override public void OnClick (View v) {switch (V.getid ()) {case r.id.sendbroadcast: {//Send broadcast
Sendbroadcastreceiver (Mtelephone, Mcontext);
Break
} Default:break; }/** * Registration broadcast/public void Registerbroadcast () {//send intentfilter intentfilters = new Intentfil
ter (action_sendmessage);
Mhelpers = new Broadcastreceiverhelper (mainactivity.this);
This.registerreceiver (Mhelpers, intentfilters);
Receive Intentfilter intentfilterd = new Intentfilter (action_delivermessage);
Mhelperd = new Broadcastreceiverhelper (mainactivity.this);
This.registerreceiver (Mhelperd, intentfilterd); /** * Send broadcast SMS send the process: Send the time need to send a broadcast once, receive the time need to send a broadcast/public void Sendbroadcastreceiver (string telephone, string
Content) {if (telephone!= null) {Intent sintent = new Intent (action_sendmessage);
SMS sent after the successful transmission of the broadcast pendingintent sentintent = Pendingintent.getbroadcast (mainactivity.this, 0, sintent, 0);
Intent dintent = new Intent (action_delivermessage); Message received before sending the broadcast pendingintent deliveryintent = Pendingintent.getbroadcast (mainactivity.this, 0, dintent, 0
);
Msmsmanager.sendtextmessage (telephone, NULL, content, sentintent, deliveryintent);
This.sendbroadcast (Intent);
}
}
}
Note: You need to add the following permissions:
<!--SMS Privileges-->
<uses-permission android:name= "Android.permission.SEND_SMS" >
</ uses-permission>
<uses-permission android:name= "Android.permission.READ_SMS" >
</ uses-permission>
<uses-permission android:name= "Android.permission.RECEIVE_SMS" >
</ Uses-permission>