Android/ophone development handout (8): What should I do when I receive a text message?

Source: Internet
Author: User
Ben And The content of roid/ophone development handout is serialized. Reprinted please indicate the source

SMS is a service that is frequently used in mobile phones. However, when a mobile phone receives a text message, it also sends a broadcast to the system. If our applicationProgramTo do something after the phone receives the text message, you need to receive the system broadcast.
We are already familiar with the process of receiving system broadcasts. Next we will perform these two steps step by step.
1. Write an smsreceiver class to receive system broadcasts.

Package Net. blogjava. Mobile. SMS;

ImportAndroid. content. broadcastreceiver;
ImportAndroid. content. context;
ImportAndroid. content. intent;
ImportAndroid. OS. Bundle;
ImportAndroid. telephony. GSM. smsmessage;
ImportAndroid. widget. Toast;

Public Class Smsreceiver Extends Broadcastreceiver
{
@ Override
Public Void Onreceive (context, intent)
{
// Determines whether the received broadcast is the broadcast action that receives the SMS.
If ( " Android. provider. telephony. sms_received " . Equals (intent. getaction ()))
{
Stringbuilder sb = New Stringbuilder ();
// Receive data sent from SMS
Bundle bundle = Intent. getextras ();
// Determine whether data exists
If (Bundle ! = Null )
{
// All received SMS messages can be obtained through PDUS.
Object [] objarray = (Object []) bundle. Get ( " PDUS " );
// Construct the SMS object array and create the size of the array based on the length of the received object.
Smsmessage [] messages = New Smsmessage [objarray. Length];
For ( Int I = 0 ; I < Objarray. length; I ++ )
{
Messages = Smsmessage. createfrompdu (( Byte []) Objarray );
}
// Combine the sent text message with custom information in stringbuilder
For (Smsmessage currentmessage: messages)
{
SB. append ( " SMS Source: " );
// Obtain the phone number for receiving the SMS
SB. append (currentmessage. getdisplayoriginatingaddress ());
SB. append ( " \ N ------ text message content ------ \ n " );
// Get text message content
SB. append (currentmessage. getdisplaymessagebody ());
}
}
Intent mainintent = New Intent (context, Main. Class );
Mainintent. setflags (intent. flag_activity_new_task );
Context. startactivity (mainintent );
// Use the toast message prompt box to display the received SMS content
Toast. maketext (context, SB. tostring (), Toast. length_long). Show ();
}
}
}

Note the following four points when writing the smsreceiver class:

    • The broadcast action for Receiving text messages is Android. provider. telephony. sms_received. Therefore, you must determine whether the received broadcast action is the one for Receiving text messages at the beginning of the onreceiver method.
    • You must use bundle. Get ("PDUS") to obtain the received SMS message. This method returns an array of text message content. Each array element represents a text message. This means that the bundle. Get ("PDUS") can return the SMS content received by multiple systems.
    • Arrays returned by bundle. Get ("PDUS") cannot be directly used. You need to use the smsmessage. createfrompdu method to convert these array elements into smsmessage objects. Each smsmessage object represents a text message.
    • You can use the getdisplayoriginatingaddress method of the smsmessage class to obtain the phone number of the SMS. You can use the getdisplaymessagebody method to obtain the text message content.

2. Configure the smsreceiver class in the androidmanifest. xml file,CodeAs follows:

< Cycler Android: Name = "Smsreceiver" >
< Intent-Filter >
<! -- Specifies the broadcast action that smsreceiver can receive. -->
< Action Android: Name = "Android. provider. telephony. sms_received" />
</ Intent-Filter >
</ Cycler >

To enable the application to successfully receive the sms_received broadcast, you also need to use the <uses-Permission> label to enable the application to receive text messages. The Code is as follows:

< Uses-Permission Android: Name = "Android. Permission. receive_sms" > </ Uses-Permission >

Start the application now. The page displays the message "waiting to receive text messages. There is another problem. What if we test this program on the simulator?
It is not difficult to solve this problem. The Android simulator can not only simulate the running of programs, but also simulate many mobile phone actions. For example, text messaging is one of them. To simulate mobile phone movements, you still need to turn to the ddms perspective. In the ddms perspective, there is a [emulator control] view (If this view is not available in ddms, please use the [Window]> [show view] menu item of eclipse to display this view ). In the telephone actions grouping box, select the SMS option box, enter a phone number in the incomingnumber text box, and then enter the text message content to be sent in the message text box, finally, click send to simulate text message sending. Enter the corresponding information, as shown in view 1. After you click the send button, the mobile phone simulator receives the SMS. no matter whether the application that receives the SMS is started or not, the toast message prompt box shown in Figure 2 is displayed.

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.