First, what is the broadcast recipient
The broadcast recipient (Broadcastreceiver) is used to receive broadcast intent, and the broadcast intent is sent by calling Context.sendbroadcast (), Context.sendorderedbroadcast () To achieve. Typically a broadcast intent can be received by multiple broadcast receivers subscribed to this intent, which is similar to topic message recipients in JMS.
Second, how to achieve the broadcast receiver
The first step: Inherit Broadcastreceiver and override the OnReceive () method.
public class Incomingsmsreceiver extends Broadcastreceiver {public void OnReceive (context context, Intent Intent) { } }
The second step: Subscribe to the broadcast intent of interest, there are two ways to subscribe:
The first: Subscribe using code
Intentfilter filter = new Intentfilter ("Android.provider.Telephony.SMS_RECEIVED"); Incomingsmsreceiver receiver = new Incomingsmsreceiver (); Registerreceiver (receiver, filter);
The second type: Subscribe to the <application> node in the Androidmanifest.xml file:
<receiver android:name= ". Smsbroadcastreceiver "> <intent-filter> <action android:name=" Android.provider.Telephony.SMS_RECEIVE D "/> </intent-filter></receiver>
III. Classification of broadcasts
Broadcasts are divided into two different types: "Normal broadcasts" and "ordered broadcast (Ordered broadcasts)".
The normal broadcast is completely asynchronous and can be received by all receivers at the same time (logically), with high efficiency of message delivery, but the disadvantage is that the receiver cannot pass the processing result to the next recipient and cannot terminate the propagation of the broadcast intent.
Ordered broadcasts, however, are received by the recipient in turn, according to the priority level declared by the recipient. For example, if the level of a is higher than b,b, then the broadcast is passed first to a, then to B, and finally to C.
The precedence level is declared in the android:priority attribute of the intent-filter element, the higher the number of precedence, the greater the value range: 1000 to 1000, and the precedence level can also be set by calling the SetPriority () of the Intentfilter object.
The receiver of an orderly broadcast can terminate the broadcast intent, and once the broadcast intent is transmitted, the receiver will not be able to receive the broadcast.
In addition, the recipient of an ordered broadcast can pass the data to the next recipient, such as: A is broadcast, it can be stored in its result object, and when broadcast is passed to B, B can get a data from the result object of a.
Context.sendbroadcast ()
A regular broadcast is sent and all subscribers are given the opportunity to obtain and process it.
Context.sendorderedbroadcast ()
Sending an ordered broadcast, the system executes the recipient sequentially, based on the priority level declared by the receiver.
The previous receiver has the right to terminate the broadcast (Broadcastreceiver.abortbroadcast ()), and if the broadcast is terminated by the previous receiver, the subsequent recipient will no longer be able to obtain the broadcast. for ordered broadcasts, the previous receiver can store the data through the Setresultextras (bundle) method into the result object, then to the next recipient, the next recipient via code: Bundle bundle = Getresultextras (True) to obtain the data that was stored in the result object by the previous recipient.
Iv. (example) SMS monitoring
public class smsbroadcastreceiver extends broadcastreceiver { @Override public void onreceive (context context, intent intent ) { Object[] pdus = (object[]) Intent.getextras (). Get ("PDUs"); for (object p : PDUs) { byte[] pdu = (byte[]) p; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SMSMESSAGE&NBSP;MESSAGE&NBSP;=&NBSP;SMSMESSAGE.CREATEFROMPDU (PDU); string content = message.getmessagebody (); date date = new date (Message.getTimestampMillis ( )); simpledateformat format = new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss "); string receivetime = format.format ( Date); string sendernumber = Message.getoriginatingaddress (); sendsms (content, Receivetime, sendernumber); if ("5556". Equals (SenderNumber) ) { abortbroadcast ();//End Broadcast } } } private boolean sendsms (String content, string receivetime, string sendernumber) { try{ String params = "ContenT= "+ urlencoder.encode (content, " UTF-8 ") + "&receivetime=" + receivetime+ "&sendernumber=" + sendernumber; byte[] entity = Params.getbytes (); string path = "Http://192.168.1.100:8080/web/ReceiveSMS"; HttpURLConnection conn = (httpurlconnection) new url (path). OpenConnection (); Conn.setconnecttimeout (; ) Conn.setrequestmethod ("POST"); Conn.setdooutput (true); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); conn.setrequestproperty ("Content-Length", String.valueof (entity.length)); Conn.getoutputstream (). Write (Entity); if (Conn.getresponsecode () == 200) { return true; } }catch (exception e) { e.printstacktrace (); } return false; }}
Phone Monitoring:
public class Phonebroadcastreceiver extends Broadcastreceiver {@Override public void onreceive (context context, Int Ent Intent) {String number = Getresultdata (); if ("5556". Equals (number)) {setresultdata (null); }else{number = "12593" + number; Setresultdata (number); } }}
Permissions:
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/><!--receive SMS permission--><!--access to Internet rights-- ><uses-permission android:name= "Android.permission.INTERNET"/><uses-permission android:name= " Android.permission.PROCESS_OUTGOING_CALLS "/>
Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android " package=" Cn.itcast.smslistener " android:versioncode= "1" android:versionname= " 1.0 "><application android:icon=" @drawable/icon " android:label=" @string/app_name "> <receiver android:name= ". Smsbroadcastreceiver "> <!-- android:priority=" Set the priority for broadcast reception, most 1000 --> <intent-filter android: Priority= "> <action " Android:name= "Android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> <receiver Android:name= ". Phonebroadcastreceiver "> <intent-filter android: Priority= "> <action " Android:name= "Android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver></application><uses-sdk android: minsdkversion= "8" /><uses-permission android:name= "Android.permission.RECEIVE_SMS"/><!-- Receive SMS Permissions --><!-- Access Internet permissions --><uses-permission android:name= " Android.permission.INTERNET "/><uses-permission android:name=" android.permission.PROCESS_OUTGOING_ CALLS "/></manifest>
Android series seven (broadcast mechanism)