About SMS blacklist BroadCast

Source: Internet
Author: User

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:

 
 
  1. Package com. broadcastreceiver;
  2. Import android. content. BroadcastReceiver;
  3. Import android. content. Context;
  4. Import android. content. Intent;
  5. Import android. OS. Bundle;
  6. Import android. telephony. SmsMessage;
  7. Import android. util. Log;
  8. Public class FirstBroadCastReceiver extends BroadcastReceiver {
  9. @ Override
  10. Public void onReceive (Context context, Intent intent ){
  11. // TODO Auto-generated method stub
  12. If (intent. getAction (). equals ("android. provider. Telephony. SMS_RECEIVED ")){
  13. Bundle bundle = intent. getExtras ();
  14. StringBuilder phoneNum = new StringBuilder ();
  15. // All received information can be obtained through pdus
  16. Object [] objArray = (Object []) bundle. get ("pdus ");
  17. // Construct the SMS object array and create the array size based on the received object Length
  18. SmsMessage [] messages = new SmsMessage [objArray. length];
  19. For (int I = 0; I <objArray. length; I ++)
  20. {
  21. Messages [I] = SmsMessage
  22. . CreateFromPdu (byte []) objArray [I]);
  23. }
  24. // Combine the sent text message with custom information in StringBuilder
  25. For (SmsMessage currentMessage: messages)
  26. {
  27. // Obtain the phone number for sending the text message
  28. PhoneNum. append (currentMessage. getDisplayOriginatingAddress ());
  29. }
  30. // You can add a database query statement to check whether the number of the sent text message is in the blacklist.
  31. // 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.
  32. // Therefore, some time-consuming operations cannot be performed in BroadcastReceiver. The ANRApplication No Response dialog box is displayed on the other side.
  33. // If you need to complete a time-consuming task, you should send the Intent to the Service, which is done by the Service.
  34. // Subthreads cannot be used here, because the life cycle of BroadcastReceiver is very short, and the subthread may end before it ends.
  35. // Once BroadcastReceiver ends, the process where BroadcastReceiver is located is easily killed first when the system requires memory,
  36. // Because it belongs to a null process without any active components ). If its host process is killed, the working sub-thread is also killed.
  37. // It is unreliable to use sub-threads.
  38. If (phoneNum. toString (). equals ("254 ")){
  39. // 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.
  40. AbortBroadcast ();
  41. Log. e ("msg", "sucess !! ");
  42. } Else
  43. Log. e ("msg", "fail! ");
  44. }
  45. }
  46. }

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:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.broadcastreceiver" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.         <uses-permission android:name="android.permission.READ_SMS"></uses-permission> 
  7.         <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  9.         <activity android:name=".MainBroadCastReceiver" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <action android:name="android.intent.action.MAIN" /> 
  13.                 <category android:name="android.intent.category.LAUNCHER" /> 
  14.             </intent-filter> 
  15.         </activity> 
  16.         <receiver android:name=".FirstBroadCastReceiver"> 
  17.             <intent-filter android:priority="1000"> 
  18.                 <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> 
  19.             </intent-filter> 
  20.         </receiver> 
  21.     </application> 
  22. </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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.