Android SMS listener

Source: Internet
Author: User

The SMS listener obtains the content, time, sender, and other information of the information when it receives the information, and then processes the information, which can shield the user from seeing the information, forwarding it, or sending it to the web for processing. Exciting function ..

Let's take a look at its implementation principles.

In fact, SMS reception is implemented by the broadcast receiver in the Android system. When the system receives the SMS, it sends a broadcast intent whose action name is Android. provider. telephony. sms_received: This intent stores the text message content received by the system. We can use the name "PDUS" to get the text message content from the intent.

Broadcast can be divided into normal broadcast and ordered broadcast. Normal broadcast is completely asynchronous and can be received by all recipients at the same time (logically). It is more efficient than ordered broadcast message transmission, but its disadvantage is: the recipient cannot deliver the result to the next receiver and cannot terminate the spread of the broadcast intent. The ordered broadcast is based on the priority stated by the recipient, and the recipient accepts the broadcast in turn. The priority value is not declared in the Android: Priority attribute of <intent-filter>. The value ranges from-1000 to 1000, and the priority level can also be used to call setpriority () of the intentfilter object (). The receiver of ordered broadcast can terminate the spread of intent. Once the spread of broadcast intent is terminated, subsequent recipients cannot receive the broadcast. In addition, the receiver of ordered broadcast can transmit data to the next broadcast. For example, after a gets a broadcast, it can store data into its result object. When broadcast is passed to B, B can get the data stored by a from the result object of.

Context. sendbroadcast ()

A normal broadcast is sent, and all subscribers have the opportunity to obtain and process it.

Context. sendorderedbroadcast ()

An ordered broadcast is sent. The system executes the recipients one by one based on the priority level stated by the recipients. The previous recipients have 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 = 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 users 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.

 

The following example shows how to implement this function:

First, add the following permissions to the configuration list:

1 <uses-Permission Android: Name = "android. Permission. receive_sms"/> <! -- Receive SMS permission --> 2 <! -- Internet access permission --> 3 <uses-Permission Android: Name = "android. Permission. Internet"/>

In the application, add the receive for processing the received information and set its priority. Unlike the activity, we add the receiver node to indicate the broadcast receiver:

1 <Cycler Android: Name = ". smsbroadcastreceiver"> 2 <intent-filter Android: Priority = "1000"> <! -- Set the highest priority --> 3 <action Android: Name = "android. provider. telephony. sms_received"/> 4 </intent-filter> 5 </Cycler>

   

The smsbroadcastreceiver we created above is not a subclass of activity, but inherits from broadcastreceiver. Because we are listeners, do not select create activity when creating a project, and do not need to create activity:

    

The core code is very simple. You only need to obtain the content in the onreceiver function when the information is received and process the content:

1 public class smsbroadcastreceiver extends broadcastreceiver {2 3/* (non-javadoc) 4 * @ see android. content. broadcastreceiver # onreceive (Android. content. context, android. content. intent) 5 * execute this method 6 */7 @ override 8 Public void onreceive (context, intent) when receiving information) {9 // accept information from intent 10 object [] PDUS = (object []) intent. getextras (). get ("PDUS"); 11 for (Object P: PDUS) {12 byte [] SMS = (byte []) P; 13 smsmessage message = smsmessage. createfrompdu (SMS); 14 15 // get sms content 16 string content = message. getmessagebody (); 17 18 // get sending time 19 Date = new date (message. gettimestampmillis (); 20 21 // obtain the sender number 22 string number = message. getoriginatingaddress (); 23
// Process data...
24 // terminate broadcast 25 // abortbroadcast (); 26} 27} 28 29}

We only obtained the data above. You can use the previous blog to forward text messages or put them in WebService for data processing.

The last line of code abortbroadcast () is used to terminate the broadcast. For example, if the phone number is blacklisted or the phone number you do not want to broadcast is terminated, at this time, because our priority is the highest, we will not receive the prompt information in the above prompt bar after the spread is not stopped, and the phone will not receive the information.

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.