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.
A pdu is a "protocol discription unit", which is the industry format for an SMS message. because SMSMessage reads/writes them you shouldn't need to disect them. A large message might be broken into vertex, which is why it is an array of objects.
Public class SMSReceiver extends BroadcastReceiver
{
/* This method is triggered when you receive the text message */
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]);
}
// Generate a Toast
Toast toast = Toast. makeText (context, "SMS content:" + smsMessage [0]. getMessageBody (), Toast. LENGTH_LONG );
// Set the toast display position
// Toast. setGravity (Gravity. TOP | Gravity. LEFT, 0,200 );
// Display the Toast
Toast. show ();
}
}
Author: Turning to tears