Android Tutorial

(avery) #1
Android Tutorial 405

This block of code is placed within the onCreate() method of the
Activity. First, the message Bundle is retrieved. In it, an array of
Objects holds several byte arrays that contain PDU data—the data
format that is customarily used by wireless messaging protocols.
Luckily, the Android SDK can decode these with a call to the static
SmsMessage.createFromPdu() utility method. From here, we can
retrieve the body of the SMS message by calling
getDisplayMessageBody().

The message that comes in might be longer than the limitations for
an SMS. If it is, it will have been broken up in to a multipart
message on the sending side. To handle this, we loop through each
of the received Object parts and take the corresponding body from
each, while only taking the sender address from the first.

Next, the code updates the text string in the TextView to show the
user the received message. The sender address is also updated so
that the recipient can respond with less typing. Finally, we register
the BroadcastReceiver with the system. The IntentFilter used here,
android.provider.Telephony.SMS_RECEIVED, is a well-known but
undocumented IntentFilter used for this. As such, we have to use
the string literal for it.

Making and Receiving Phone Calls

It might come as a surprise to the younger generation (they usually
just text), but phones are often still used for making and receiving
phone calls. Any application can be made to initiate calls and
answer incoming calls; however, these abilities should be used
Free download pdf