Android API: SMS Service Processing

Source: Internet
Author: User

Many new applications consider using SMS as a data distribution platform. Real-world scenario: the on-demand movie system requires users to send text messages in certain formats for automatic on-demand videos. Nowadays, more and more applications use SMS for data exchange with users. Now let's take a look at how we construct this form of application on the Android platform.

Android APIs support the development of applications that can send and receive SMS messages. Currently, the android simulator we use in the development process does not support sending SMS, but it can receive SMS. Now let's explore Android's support for SMS. We will build a small application to listen to the SMS messages received on mobile devices (or simulators) and display them.

Let's define an intent receiver to handle the SMS Receiving Event:

Java code

Package COM. wissen. SMS. receiver;/*** this class will be called when the SMS receives */public class smsreceiver extends broadcastreceiver {@ override public void onreceive (context, intent) {// todo }}

We need to configure the intent receiver so that it can get the SMS receiving event. The event status 'android. provider. telephony. sms_received ed' indicates that the SMS has been received. We can configure androidmanifest. XML as follows:

XML Code

<receiver android:name=”.receiver.SMSReceiver” android:enabled=”true”> <intent-filter> <action android:name=”android.provider.Telephony.SMS_RECEIVED” /> </intent-filter> </receiver>   

To enable our applications to receive SMS messages, you must specify the permissions first. You can configure the following in androidmanifest. xml:

XML Code

<uses-permission android:name=”android.permission.RECEIVE_SMS”></uses-permission> 

Now, our intent receiver can be called when the Android device receives the SMS, and the rest is to get and display the received SMS message text:

Java code

public void onReceive(Context context, Intent intent) {         Bundle bundle = intent.getExtras();         Object messages[] = (Object[]) bundle.get(”pdus”);         SmsMessage smsMessage[] = new SmsMessage[messages.length];         for (int n = 0; n < messages.length; n++) {                 smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);        }       // show first message       Toast toast = Toast.makeText(context, “Received SMS: ” + smsMessage[0].getMessageBody(),                Toast.LENGTH_LONG);       toast.show(); }   

The SMS received by the Android device is in the form of a PDU (protocol description unit ). Android. telephony. GSM. the smsmessage class can store SMS-related information. We can also create a smsmessage instance from the received PDU, the toast interface component displays the received SMS message text in the form of system notifications.

Run the program:

Now let's run this application in the simulator and send SMS messages to this simulator. In the ddms view (Dalvik debug monitor service) provided by the android plug-in of Eclipse, we can send SMS messages to the simulator (in the 'emulator control' panel; in addition, you need to specify the telephone number, but it can be any one)

 

Many new applications consider using SMS as a data distribution platform. Real-world scenario: the on-demand movie system requires users to send text messages in certain formats for automatic on-demand videos. Nowadays, more and more applications use SMS for data exchange with users. Now let's take a look at how we construct this form of application on the Android platform.

Android APIs support the development of applications that can send and receive SMS messages. Currently, the android simulator we use in the development process does not support sending SMS, but it can receive SMS. Now let's explore Android's support for SMS. We will build a small application to listen to the SMS messages received on mobile devices (or simulators) and display them.

Let's define an intent receiver to handle the SMS Receiving Event:

Java code

Package COM. wissen. SMS. receiver;/*** this class will be called when the SMS receives */public class smsreceiver extends broadcastreceiver {@ override public void onreceive (context, intent) {// todo }}

We need to configure the intent receiver so that it can get the SMS receiving event. The event status 'android. provider. telephony. sms_received ed' indicates that the SMS has been received. We can configure androidmanifest. XML as follows:

XML Code

<receiver android:name=”.receiver.SMSReceiver” android:enabled=”true”> <intent-filter> <action android:name=”android.provider.Telephony.SMS_RECEIVED” /> </intent-filter> </receiver>   

To enable our applications to receive SMS messages, you must specify the permissions first. You can configure the following in androidmanifest. xml:

XML Code

<uses-permission android:name=”android.permission.RECEIVE_SMS”></uses-permission> 

Now, our intent receiver can be called when the Android device receives the SMS, and the rest is to get and display the received SMS message text:

Java code

public void onReceive(Context context, Intent intent) {         Bundle bundle = intent.getExtras();         Object messages[] = (Object[]) bundle.get(”pdus”);         SmsMessage smsMessage[] = new SmsMessage[messages.length];         for (int n = 0; n < messages.length; n++) {                 smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);        }       // show first message       Toast toast = Toast.makeText(context, “Received SMS: ” + smsMessage[0].getMessageBody(),                Toast.LENGTH_LONG);       toast.show(); }   

The SMS received by the Android device is in the form of a PDU (protocol description unit ). Android. telephony. GSM. the smsmessage class can store SMS-related information. We can also create a smsmessage instance from the received PDU, the toast interface component displays the received SMS message text in the form of system notifications.

Run the program:

Now let's run this application in the simulator and send SMS messages to this simulator. In the ddms view (Dalvik debug monitor service) provided by the android plug-in of Eclipse, we can send SMS messages to the simulator (in the 'emulator control' panel; in addition, you need to specify the telephone number, but it can be any one)

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.