0% found this document useful (0 votes)
14 views28 pages

SMSlectue

Uploaded by

Abdullah Munir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views28 pages

SMSlectue

Uploaded by

Abdullah Munir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Integrating

with Android
Services
Ø Android has numerous built-in functionality that can be
called from within your applications
Ø SMS/MMS
Ø Telephony (calls)
Ø GPRS/3G
Ø WIFI

Ø These functions can be seamlessly integrated into your


apps
Ø Due to the use of Intents
Ø Justlike with earlier topics requiring file
system access many built-in functions
require the use of permissions
Ø These are needed to inform the user what
sort of features an application requires
Ø Very important if you download apps,
e.g. from the Android Market
SMS
Ø Message types
Ø Text (max 160 characters/message)
Ø Binary (max 140 bytes/message)

Ø Multi-part messages
Ø if a message is too large it can be sent as several parts and
reassembled at the destination
Ø each part is a separate message with special information indicating the
different parts form one whole message
Ø Usually, only a maximum of 3 parts is recommended since parts
can be lost in transit
Ø If a part is lost, essentially the message is now useless
Ø Port number
Ø a special value indication what type of application is going to use
the message
Ø e.g. sending vCard via SMS uses port 9204
Ø Applications can define their own port number for their specific
purposes
Overview
Ø Android
provides a means to access the phone’s
SMS service
Ø Applications can both send and receive SMS messages
Ø Port-based messages are also supported on
device
Ø Sending of ported and non-ported sms is supported
Ø However, the emulator does not properly handle
receiving port-based SMS (they never arrive)
Ø Non-port based messages are properly handled
Ø They can be received with no problem
Ø To be able to access SMS you need to
make sure the following permissions
are placed in your AndroidManifest.xml
Ø For sending:
<uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>

Ø For receiving:
<uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission>
Ø To
send SMS on Android you need to
use the following classes
Ø SmsManager
Ø PendingIntent
Ø SmsManager is a system class that
holds the instance referencing the
phone’s SMS system
Ø android.telephony.SmsManager

Ø You do not create instances of this class; it


is a singleton
Ø You refer to this instance using the static
getDefault() method
Ø SmsManager has several methods for
sending SMS
Ø Each is used for specific circumstances
Ø sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sen
tIntent, PendingIntent deliveryIntent)
Ø Send a text based SMS.
Ø sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String>
parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)
Ø Send a multi-part text based SMS.
Ø sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[]
data, PendingIntent sentIntent, PendingIntent deliveryIntent)
Ø Send a data based SMS to a specific application port
Ø The simplest way to send a message is
to use sendTextMessage()
Ø Itmakes the assumption that the
message is at most 160 characters
long
Ø Anything beyond that will be truncated and
lost
Ø sendTextMessage has several parameters:
Ø String destinationAddress – phone number to send to
Ø String scAddress – smsc address (usually null)
Ø String text – message that will be sent
Ø PendingIntent sentIntent – intent that will be triggered
when the message is sent
Ø PendingIntent deliveryIntent – intent that will be triggered
when the message is delivered
ØA PendingIntent is a special kind of intent in that it
is deferred
Ø The intent is triggered after a specific condition
is met
Ø E.g. when message is sent or delivered

Ø Thereare two ways of getting PendingIntents


used for SMS
Ø PendingIntent.getActivity()
Ø PendingIntent.getBroadcast()
PendingIntent.getActivity()
Ø This is used to signal that you want to open a specific
activity when this PendingIntent is triggered
Ø This method has several parameters
Ø Context - The Context in which this PendingIntent should
start the activity.
Ø requestCode - (currently not used).
Ø intent - Intent of the activity to be launched.
Ø flags - any of the flags as supported by Intent.fillIn() to
control which unspecified parts of the intent that can be
supplied when the actual send happens (usually just 0)
PendingIntent pi = PendingIntent.getActivity(this,
0,
new Intent(this, Sms1Activity.class),
0);

NOTE: the intent used will be triggered


using startActivity() with this being the
context for the activity
private void sendSMS(String phoneNumber, String message)
{
// this will re-open the Sms1Activity upon completion
PendingIntent pi = PendingIntent.getActivity(this,
0,
new Intent(this, Sms1Activity.class),
0);

SmsManager sms = SmsManager.getDefault();


sms.sendTextMessage(phoneNumber,
null, // null == use default SMSC
message,
pi, // sent intent
null);
}
Ø Status updates for services are send out using a broadcast
Ø You need to specify a broadcast receiver to listen for these
broadcasts
Ø Broadcast receivers are all sub-types of the
BroadcastReceiver class
Ø android.content.BroadcastReceiver
Ø Defines an onReceive() method

Ø These must then be registered with the Context/Activity in


order for them to be triggered
private class DeliveredBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}
Ø The values returned by getResultCode() are defined by the Broadcast being
made
private class SentBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
break;

// these constants are defined in the SmsManager


case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
}
}
}
Unregistering receivers
Ø Receivers must be unregistered from
an activity when the activity finishes.
Ø unregisterReceiver(receiver)
Ø NOTE: this means you may need to hold
on to references of the receivers so you
can pass them as parameters to this
method
Example
public void onBackPressed()
{
back();
}

private void back()


{
finish();

// must remove receivers or else and error will occur


unregisterReceiver(sentBroadcastReceiver);
}

ØA good place to put this where the back behaviour


is done or where you close the Activity
Ø Multipartmessages are handled in a
similar fashion to single messages
Ø However,you will need to split the long
message into smaller parts
Ø Use SmsManager.divideMessage(String)
Ø This will return an ArrayList<String>
containing the individual Strings to be sent
Ø Like
single messages, multipart
messages use PendingIntents to
handle post-send/delivery actions
Ø Since multiple messages are used,
multiple intents are also used per part
Ø These are passed as an ArrayList
Ø See example
Ø sendMultipartTextMessage has several parameters:
Ø String destinationAddress – phone number to send to
Ø String scAddress – smsc address (usually null)
Ø String text – message that will be sent
Ø ArrayList<PendingIntent> sentIntents – intents that will
be triggered when each part is sent
Ø ArrayList<PendingIntent> deliveryIntents – intents that
will be triggered when each part is delivered
Ø Usually sending SMS is done in a
separate thread to insure the UI thread
is not blocked by the action
Ø Thisis particularly important with
multipart messages if you want to track
the progress of the sending process
Upcoming
Ø In the next lecture:
Ø Threaded updates using Handler
Ø Receiving SMS messages
Ø Using multiple emulators to simulate
multiple phones
Ø Programmatically triggering a call
Ø Androidis very specific as to which
thread alters the contents of the UI
Ø Only the UI thread is allowed to touch the
UI related objects
Ø Any other thead doing this will trigger an
Exception
Ø Updates from another thread are done
via a Handler

You might also like