First, receive broadcast messages
1. What is Broadcastreceiver?
Broadcastreceiver is one of the four components of the Android system, essentially a global listener for listening to the system's global broadcast messages.
2. How do I create a broadcastreceiver?
1) Create a class that inherits from the Broadcastreceiver class;
2) in Androidmanifest.xml Configuration <receiver> node information.
3. Once the Broadcastreceiver has been implemented, the next step should be to develop the intent that the Broadcastreceiver can match, at which time there are two ways.
1) configured in the Androidmanifest.xml file, this way will always listen. For example, the following code:
<receiver android:name= "Com.gnnuit.smsreceiver.SmsReceiver" > <intent-filter android:priority= "1000" > <action android:name= "Android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
2) using the code to specify, call the Broadcastreceiver context of the Registerreceiver (Broadcastreceiver receiver, Intentfilter filter) method specified, This method can be canceled by Unregisterreceiver (Broadcastreceiver receiver), also known as temporary monitoring. For example, the following code:
Intentfilter filter=New intentfilter ("Android.provider.Telephony.SMS_RECEIVED"); Registerreceiver (new smsreceiver (), filter);
4. If the Broadcastreceiver onreceive () method cannot be completed within 10 seconds, Android will consider the program unresponsive. So do not perform some time-consuming operations in the Broadcastreceiver OnReceive () method, or the ANR exception will occur. If you do need to do some time-consuming operations based on broadcast, you may want to consider starting a service with intent to complete the operation. You should not consider using a new thread to complete the time-consuming operation because the broadcast itself has a short life cycle.
The 5.Broadcast is divided into the following two types:
1) normal broadcast (normal broadcast): The general broadcast is completely asynchronous and can be received by all receivers at the same time (logically), with high efficiency of message delivery. However, the disadvantage is that the recipient cannot pass the processing result to the next recipient and cannot terminate the propagation of the broadcast intent.
2) Ordered broadcast (ordered broadcast): The recipient of an ordered broadcast will receive broadcast in order of pre-declared priority. The precedence level is declared in the android:priority attribute of the <intent-filter/> element, the higher the number, the greater the priority, and the value range of-1000-1000. Ordered broadcasts can call Abortbroadcast () Method terminates the propagation of the broadcast intent, and once the propagation of the broadcast intent is terminated, the subsequent recipient cannot receive the broadcast.
6. Two ways to send a broadcast:
1) sendbroadcast (): Send General broadcast.
2) sendorderedbroadcast (): Send an ordered broadcast.
7. SMS Interception Example
Smsreceiver.java
PackageCom.gnnuit.smsreceiver;Importjava.util.Date;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.telephony.SmsMessage; Public classSmsreceiverextendsBroadcastreceiver {//triggered when a text message is received@Override Public voidOnReceive (Context context, Intent Intent) {System.out.println ("Receive SMS!!!" "); Object[] PDUs= (object[]) Intent.getextras (). Get ("PDUs");//Receive all SMS messages via PDUs for(Object pdu:pdus) {smsmessage message= SMSMESSAGE.CREATEFROMPDU ((byte[]) PDU); String content= Message.getmessagebody ();//Get SMS ContentString number = message.getoriginatingaddress ();//get the SMS numberDate Date =NewDate (Message.gettimestampmillis ());//Get SMS DateSYSTEM.OUT.PRINTLN (number + "," + Content + "," +date); if("110". Equals (number)) {abortbroadcast ();//Cancel the broadcast so that the system does not receive text messages } } }}
Androidmanifest.xml
<receiver android:name= "Com.gnnuit.smsreceiver.SmsReceiver" > <intent-filter android:priorit y= "> <action android:name=" Android.provider.Telephony.SMS_RECEIVED "/> </inten T-filter> </receiver>