28
Integrating with Android Services

Integrating with Android Services. Introduction Android has numerous built-in functionality that can be called from within your applications SMS/MMS

Embed Size (px)

Citation preview

Page 1: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Integratingwith Android

Services

Page 2: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

IntroductionIntroduction

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

Page 3: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

PermissionsPermissions

Just like with earlier topics requiring file system access many built-in functions require the use of permissionsThese are needed to inform the user what

sort of features an application requiresVery important if you download apps,

e.g. from the Android Market

Page 4: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

SMS

Page 5: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Basic SMS ConceptsBasic SMS Concepts

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

Page 6: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Basic SMS ConceptsBasic SMS Concepts

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

Page 7: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

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

Page 8: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

PermissionsPermissions

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>

Page 9: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Sending SMSSending SMS

To send SMS on Android you need to use the following classesSmsManagerPendingIntent

Page 10: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

SmsManagerSmsManager

SmsManager is a system class that holds the instance referencing the phone’s SMS systemandroid.telephony.SmsManager

You do not create instances of this class; it is a singleton

You refer to this instance using the static getDefault() method

Page 11: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

SmsManagerSmsManager

SmsManager has several methods for sending SMSEach is used for specific circumstances

sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, 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

Page 12: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Simple sendingSimple sending

The simplest way to send a message is to use sendTextMessage()

It makes the assumption that the message is at most 160 characters longAnything beyond that will be truncated and

lost

Page 13: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

sendTextMessage()sendTextMessage()

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

Page 14: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

PendingIntentPendingIntent

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

There are two ways of getting PendingIntents used for SMS PendingIntent.getActivity() PendingIntent.getBroadcast()

Page 15: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

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)

Page 16: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

ExampleExample

NOTE: the intent used will be triggered using startActivity() with this being the context for the activity

PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Sms1Activity.class), 0);

Page 17: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

ExampleExample

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);

}

Page 18: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Listening for SMS statusListening for SMS status

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

Page 19: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

BroadcastReceiverBroadcastReceiver

The values returned by getResultCode() are defined by the Broadcast being made

private class DeliveredBroadCastReceiver extends BroadcastReceiver {@Overridepublic 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; }}}

Page 20: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

ExampleExampleprivate class SentBroadcastReceiver extends BroadcastReceiver {@Overridepublic 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;}}}

Page 21: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

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

Page 22: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Example

A good place to put this where the back behaviour is done or where you close the Activity

public void onBackPressed() { back(); }

private void back() {finish();

// must remove receivers or else and error will occurunregisterReceiver(sentBroadcastReceiver);}

Page 23: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Multipart MessagesMultipart Messages

Multipart messages are handled in a similar fashion to single messages

However, you will need to split the long message into smaller partsUse SmsManager.divideMessage(String)This will return an ArrayList<String>

containing the individual Strings to be sent

Page 24: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Multipart MessagesMultipart Messages

Like single messages, multipart messages use PendingIntents to handle post-send/delivery actions

Since multiple messages are used, multiple intents are also used per partThese are passed as an ArrayListSee example

Page 25: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

sendMultipartTextMessage()sendMultipartTextMessage()

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

Page 26: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

ThreadsThreads

Usually sending SMS is done in a separate thread to insure the UI thread is not blocked by the action

This is particularly important with multipart messages if you want to track the progress of the sending process

Page 27: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Upcoming

In the next lecture:Threaded updates using HandlerReceiving SMS messages

Using multiple emulators to simulate multiple phones

Programmatically triggering a call

Page 28: Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS

Threaded updatesThreaded updates

Android is very specific as to which thread alters the contents of the UIOnly the UI thread is allowed to touch the UI

related objectsAny other thead doing this will trigger an

Exception

Updates from another thread are done via a Handler