SeeAndroidBroadcast is divided into two different types:Normal broadcast Normal broadcasts) "and" Ordered broadcast Ordered broadcasts)". Normal broadcast is completely asynchronous and can be received by all receivers at the same time logically. The message transmission efficiency is relatively high, but the disadvantage is: the receiver cannot pass the processing result to the next receiver, and cannot terminate the propagation of the broadcast Intent. However, the ordered broadcast is based on the priority level stated by the receiver, and the receiver receives the broadcast in sequence. For example, if the level of A is higher than that of B and the level of B is higher than that of C, broadcast is first transmitted to A, then to B, and finally to C. Priority Level Declaration is in the android: priority attribute of intent-filter element. The higher the number, the higher the priority level. value range:-1000 to 1000. You can also call setPriority () of the IntentFilter object at the priority level (). The receiver of an ordered broadcast can terminate the spread of the broadcast Intent. Once the spread of the broadcast Intent is terminated, the subsequent receiver cannot receive the broadcast. In addition, the receiver of ordered broadcast can pass the data to the next receiver. For example, after A gets the broadcast, it can store the data in its result object. When broadcast is sent to B, B can obtain the data stored by A from the result object of.
Context. sendBroadcast () sends a normal broadcast. All subscribers have the opportunity to obtain and process the broadcast.
Context. sendOrderedBroadcast () sends an ordered broadcast. The system executes the receivers one by one based on the priority level stated by the receiver. The previous receiver has the right to terminate the broadcast (BroadcastReceiver. abortBroadcast (), if the broadcast is terminated by the previous receiver, the subsequent receiver will no longer be able to obtain the broadcast. For ordered broadcast, the previous receiver can store the data in the result object through the setResultExtras (Bundle) method, and then pass it to the next receiver. The next receiver uses the code: Bundle bundle = getResultExtras (true )) you can obtain the data stored by the previous receiver in the result object.
When the system receives a text message, the broadcast is an ordered broadcast. If you want to prevent the user from receiving text messages, you can set the priority so that the custom receiver can first obtain the broadcast and then terminate the broadcast so that the user cannot receive the text message.
Therefore, you can use broadcast to implement the blacklist function:
- Package com. broadcastreceiver;
- Import android. content. BroadcastReceiver;
- Import android. content. Context;
- Import android. content. Intent;
- Import android. OS. Bundle;
- Import android. telephony. SmsMessage;
- Import android. util. Log;
- Public class FirstBroadCastReceiver extends BroadcastReceiver {
- @ Override
- Public void onReceive (Context context, Intent intent ){
- // TODO Auto-generated method stub
- If (intent. getAction (). equals ("android. provider. Telephony. SMS_RECEIVED ")){
- Bundle bundle = intent. getExtras ();
- StringBuilder phoneNum = new StringBuilder ();
- // All received information can be obtained through pdus
- Object [] objArray = (Object []) bundle. get ("pdus ");
- // Construct the SMS object array and create the array size based on the received object Length
- SmsMessage [] messages = new SmsMessage [objArray. length];
- For (int I = 0; I <objArray. length; I ++)
- {
- Messages [I] = SmsMessage
- . CreateFromPdu (byte []) objArray [I]);
- }
- // Combine the sent text message with custom information in StringBuilder
- For (SmsMessage currentMessage: messages)
- {
- // Obtain the phone number for sending the text message
- PhoneNum. append (currentMessage. getDisplayOriginatingAddress ());
- }
- // You can add a database query statement to check whether the number of the sent text message is in the blacklist.
- // Note that the maximum running time of the onReceive () method is 10 seconds. Android considers that the program has no response if it exceeds 10 seconds.
- // Therefore, some time-consuming operations cannot be performed in BroadcastReceiver. The ANRApplication No Response dialog box is displayed on the other side.
- // If you need to complete a time-consuming task, you should send the Intent to the Service, which is done by the Service.
- // Subthreads cannot be used here, because the life cycle of BroadcastReceiver is very short, and the subthread may end before it ends.
- // Once BroadcastReceiver ends, the process where BroadcastReceiver is located is easily killed first when the system requires memory,
- // Because it belongs to a null process without any active components ). If its host process is killed, the working sub-thread is also killed.
- // It is unreliable to use sub-threads.
- If (phoneNum. toString (). equals ("254 ")){
- // If it is in the blacklist, the broadcast will be terminated. In this way, the system will not receive any prompts and the user will not receive the text message.
- AbortBroadcast ();
- Log. e ("msg", "sucess !! ");
- } Else
- Log. e ("msg", "fail! ");
- }
- }
- }
To obtain the SMS number, the permission to read the SMS is involved, and the android: priority attribute of the intent-filter element indicates the priority. A larger value indicates a higher priority.
The following is the AndroidManifest. xml code:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.broadcastreceiver"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
- <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainBroadCastReceiver"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name=".FirstBroadCastReceiver">
- <intent-filter android:priority="1000">
- <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
- </intent-filter>
- </receiver>
- </application>
- </manifest
Benefits of Android threads
Android unit test and log output
Android Network Connection processing learning notes
Ten practical tips for the Android 2.2 System
Android Activity and Intent mechanism learning notes