Brief Introduction to concepts related to Android SMS Service

Source: Internet
Author: User

Those who frequently follow our BKJIA technical articles should remember the article we introduced about the implementation of the Android text message sending function. You can learn more about the application of the SMS function. So today, you can learn some basic knowledge about the Android SMS service.

  • How to install and uninstall a program on Android
  • Android Shell commands
  • Android data transmission Overview
  • Concepts related to the Android single-thread model
  • Operation tips for the Android dialog box

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 by mobile devices or simulators and display them.

Let's define an Intent receiver to handle Android SMS message service receiving events:

 
 
  1. Package com. wissen. sms. Cycler;
  2. /**
  3. * This class will be called when the SMS receives
  4. */
  5. Public class SMSReceiver extends BroadcastReceiver {
  6. @ Override
  7. Public void onReceive (Context context, Intent intent ){
  8. // TODO
  9. }
  10. }
  11. Package com. wissen. sms. Cycler;
  12. /**
  13. * This class will be called when the SMS receives
  14. */
  15. Public class SMSReceiver extends BroadcastReceiver {
  16. @ Override
  17. Public void onReceive (Context context, Intent intent ){
  18. // TODO
  19. }
  20. }

We need to configure the Intent receiver so that it can obtain the event received by the Android SMS service. The event status 'android. provider. Telephony. sms_received' indicates that the SMS has been received. We can configure AndroidManifest. xml as follows:

 
 
  1. < receiver android:name=”.receiver.SMSReceiver” 
    android:enabled=”true”>   
  2. < intent-filter>   
  3. < action android:name=”android.provider.Telephony.
    SMS_RECEIVED” /> 
  4. < /intent-filter>   
  5. < /receiver>   
  6. < receiver android:name=”.receiver.SMSReceiver” 
    android:enabled=”true”> 
  7. < intent-filter> 
  8. < action android:name=”android.provider.Telephony.
    SMS_RECEIVED” /> 
  9. < /intent-filter> 
  10. < /receiver> 

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

 
 
  1. < uses-permission android:name=”android.
    permission.RECEIVE_SMS”> 
  2. < /uses-permission>   
  3. < uses-permission android:name=”android.
    permission.RECEIVE_SMS”> 
  4. < /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:

 
 
  1. public void onReceive(Context context, Intent intent) {   
  2. Bundle bundle = intent.getExtras();   
  3. Object messages[] = (Object[]) bundle.get(”pdus”);   
  4. SmsMessage smsMessage[] = new SmsMessage[messages.length];   
  5. for (int n = 0; n &lt; messages.length; n++) {   
  6. smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);   
  7. }   
  8. // show first message   
  9. Toast toast = Toast.makeText(context, “Received SMS: ” + 
    smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);   
  10. toast.show();   
  11. }   
  12. public void onReceive(Context context, Intent intent) {  
  13. Bundle bundle = intent.getExtras();  
  14. Object messages[] = (Object[]) bundle.get(”pdus”);  
  15. SmsMessage smsMessage[] = new SmsMessage[messages.length];  
  16. for (int n = 0; n &lt; messages.length; n++) {  
  17. smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);  
  18. }  
  19. // show first message  
  20. Toast toast = Toast.makeText(context, “Received SMS: ” + 
    smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);  
  21. 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.

Now let's run this application in the simulator and send Android 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, we need to specify the phone number, but it can be arbitrary)

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.