Android Tutorial

(avery) #1

By : Ketan Bhimani


404 

received. Further, you could use another graphic to indicate when
the recipient has received the message. Alternatively, you could
use ProgressBar widgets track the progress to the user.

Receiving an SMS

Applications can also receive SMS messages. To do so, your
application must register a BroadcastReceiver to listen for the
Intent action associated with receiving an SMS. An application
listening to SMS in this way doesn’t prevent the message from
getting to other applications.

Expanding on the previous example, the following code shows how
any incoming text message can be placed within a TextView on the
screen:

final TextView receivedMessage = (TextView)findViewById(
R.id.received_message);
rcvIncoming = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.i(“telephony”, “SMS received”);
Bundle data = intent.getExtras();
if (data != null) {
Object pdus[] = (Object[]) data.get(“pdus”);
String message = “New message:\n”;
String sender = null;
for (Object pdu : pdus) {
SmsMessage part = SmsMessage.
createFromPdu((byte[])pdu);
message += part.getDisplayMessageBody();
(sender == null) {
sender = part.
getDisplayOriginatingAddress();
}
}
receivedMessage.setText(message + “\nFrom: “+sender);
numberEntry.setText(sender);
}
}
};
registerReceiver(rcvIncoming, new IntentFilter(
“android.provider.Telephony.SMS_RECEIVED”));

Free download pdf