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:
- Package com. wissen. sms. Cycler;
- /**
- * This class will be called when the SMS receives
- */
- Public class SMSReceiver extends BroadcastReceiver {
- @ Override
- Public void onReceive (Context context, Intent intent ){
- // TODO
- }
- }
- Package com. wissen. sms. Cycler;
- /**
- * This class will be called when the SMS receives
- */
- Public class SMSReceiver extends BroadcastReceiver {
- @ Override
- Public void onReceive (Context context, Intent intent ){
- // TODO
- }
- }
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:
- < receiver android:name=”.receiver.SMSReceiver”
android:enabled=”true”>
- < intent-filter>
- < action android:name=”android.provider.Telephony.
SMS_RECEIVED” />
- < /intent-filter>
- < /receiver>
- < 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 Android SMS, you must specify the permissions first. You can configure the following in AndroidManifest. xml:
- < uses-permission android:name=”android.
permission.RECEIVE_SMS”>
- < /uses-permission>
- < 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:
- 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();
- }
- 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.
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)