Upload
zybotechsolutions
View
220
Download
0
Embed Size (px)
Citation preview
7/31/2019 Android API SMS handling
1/3
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
Android Application Development Training Tutorial
For more info visit
http://www.zybotech.in
http://www.zybotech.in/http://www.zybotech.in/http://www.zybotech.in/7/31/2019 Android API SMS handling
2/3
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
Android APISMS handling
Many new application will use SMS as data delivery platform. Reality shows, on-demand movies etc request
users to send predefined formatted SMS. Similarly some applications are coming up which sends data to user
using SMS. Lets see how such an application can be built using Android platform.
Android API support developing applications that can send and receive SMS messages. The android emulator
does not support sending of the SMS currently. But the emulator can receive SMS. Lets explore the android
SMS support and develop a small program that listens to the SMSes received on the device (on emulator) and
will show that message as notification.
The event handling on Android is done with the help of intents and intent receivers. The intents announce (or
broadcast) the event and intent receivers respond to the event. Intent receivers act as the event handlers.
Lets define an intent receiver that can handle the SMS received event:
package com.wissen.sms.receiver;/*** The class is called when SMS is received.*/public class SMSReceiver extends BroadcastReceiver {
@Overridepublic void onReceive(Context context, Intent intent) {// TODO}}
We need to configure this intent receiver to receive SMS receive event. For SMS receive event android has
defined an intent as android.provider.Telephony.SMS_RECEIVED . The receiver can be configured in
AndroidManifest.xml as follows:
To receive SMS, application also needs to specify permission for receiving SMS. The permission can be set in
AndroidManifest.xml as follows:
Now our intent receiver is all set to be called when the android device will receive SMS. Now we only need to
retrieve the received SMS and show the SMS text in a notification.
Here is the code of intent receiver that will read the SMS from intent received and show the first message
(pdu).
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");SmsMessage smsMessage[] = new SmsMessage[messages.length];
7/31/2019 Android API SMS handling
3/3
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
for (int n = 0; n < messages.length; n++) {smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);}
// show first messageToast 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 pdus (protocol description unit). Class SmsMessage,
defined in android.telephony.gsm package, can store information about the SMS. The class can also be used to
create SmsMessage object from received pdus. Toast widget is used to show the SMS body as an notification.
Running the Program:
Only remaining thing now is running the application and sending the SMS message to the emulator. An SMS
message can be sent to the emulator in the DDMS eclipse perspective (Dalvik Debug Monitor Service).
Emulator Control window can be used to send SMS message (an incoming number has to be provided whichcan be anything).
Here is the application screen shot in action,
Android SMS receiver application