Week7.pptx

COIT20270 Application Development for Mobile Platforms Week 7: SMS and email Messaging, Location Based Services

Dr. R. Balsys, CQU, 2018.

Source: Beginning Android Programming with Android Studio, J.F. DiMarzio, 2016

Week 7 – SMS and email Messaging

This week we:

Send SMS messages programmatically

Use the built in messaging application in our app

Receive SMS messages programmatically

Send emails from our applications

2

CQU - COIT20270 Application Development for Mobile Platforms

SMS Messaging

SMS messaging is a “killer app” for mobile phones

SMS messaging has billions of users in todays world

Being able to send and receive SMS messages within our applications is an attractive proposition, e.g. tracker app

You can test SMS messaging on the simulator

3

CQU - COIT20270 Application Development for Mobile Platforms

Switch to site and read through section. Do not follow any links

3

Sending SMS Messages Programmatically

You need to set <uses-permission android:name=“android.permissions.SEND_SMS”/> in your Android Manifest file

You create an instance of the SmsManager class. When declaring the instance you must use the getDefault() method to get a SmsManager object

The sendTextMessage() method on the SmsManager instance object is used to send the message

4

CQU - COIT20270 Application Development for Mobile Platforms

…sendTextMessage

sendTextMessage() has the following 5 parameters:

destinationAddress: phone number of recipient

scAddress: service Centre address or null for default

text: content of the SMS

sentIntent: pending intent to invoke when sent

deliveryIntent: pending intent to invoke when delivered

5

CQU - COIT20270 Application Development for Mobile Platforms

Using Built-in SMS app

It is simple to invoke the built in SMS app from your app. When the SMS message is sent you return to your app

Create a new Intent instance with new Intent(android.content.Intent.ACTION-VIEW) as the constructor

The putExtra() method can then be used to load the “address”, “sms_body” and “text” fields of the SMS messenger app when it loads

You must also set the MIME type of the message using setType(“vnd.android-dir/mms-sms”)

The startActivity() method is invoked with the Intent instance as parameter to start the messenger app when the setup is complete

6

CQU - COIT20270 Application Development for Mobile Platforms

Demonstrate and Explain how it works

6

Receiving SMS Messages

You need to set <uses-permission android:name=“android.permissions.RECEIVE_SMS”/> in your Android Manifest file

Receiving SMS messages is done using a BroadcastReceiver object which allows you to receive Intents using sendBroadcast(), that allows you to handle an event raised by another application

To handle the event you override the onReceive() method

The SMS message is contained in the 2nd parameter of onReceive() (an Intent object)

7

CQU - COIT20270 Application Development for Mobile Platforms

Explain how the files work

7

…Receiving SMS Messages

SMS messages are stored in an Object array in PDU format. The message is split into 160 character sub-arrays

You extract the content using the static createFromPdu() method of the SmsMessage class

The phone number of the sender is found by calling the getOriginatingAddress() method and the body of the message is found using getMessageBody()

BroadcastReceiver’s remain running even when your app is not running and so can respond to events even when the app is not running

8

CQU - COIT20270 Application Development for Mobile Platforms

Explain how the files work

8

Preventing Messaging Apps from Receiving Messages

The default behavior is that your application and the built-in SMS app get any messages

To prevent another application from receiving the message use

<intent-filter android:priority=“100”/>

<action android:name= “android.provider.Telephony.SMS_RECEIVED” /></intent-filter>

in your Android manifest file and call abortBroacast() in your onReceive() event handler

This will permanently block all incoming SMS calls

9

CQU - COIT20270 Application Development for Mobile Platforms

Explain how the files work

9

Updating Activities from BroadcastReceiver

To display an SMS message in a TextView of your activity class you create a modified SMSReceiver class that listens for a new Intent type to use for the application, eg “MY_APP_RECEIVED”

Then you override the onReceive() method of your BroadcastReceiver object to display the SMS in the TextView

You need to create an IntentFilter object so you can listen for the Intent. This is done by adding your “MY_APP_RECEIVED” parameter when you call addAction() on your new IntentFilter instance

10

CQU - COIT20270 Application Development for Mobile Platforms

Demonstrate and Explain how the files work

10

…Updating Activities from BroadcastReceiver

You call registerReceiver() in onResume() and unregisterReceiver() in onPause() so the message is only updated when the Activity is running in the foreground

11

CQU - COIT20270 Application Development for Mobile Platforms

Demonstrate and Explain how the files work

11

If your activity is in the background when a message is received it is possible to automatically bring it to the foreground to display the message

Register your BroadcastReceiver in your activities onCreate() instead of in onResume() and unregister it in your onDestroy() event rather than the onPause() event

Then modify the onReceive() event using an Intent to bring the activity to the foreground by calling Intent’s setFlag(“Intent.FLAG_ACTIVITY_NEW_CLASS”) and then stating your Intent’s instance using startActivity()

12

CQU - COIT20270 Application Development for Mobile Platforms

Invoking Activities from BroadcastReceiver

Demonstrate and Explain how the files work

12

You also need to add the tag android:launchMode=“singleTask” to the activity section of your Android manifest file. This prevents a new instance of your application being launched whenever a message is received

Using SMS messaging inside your app has various legal and ethical considerations

Once your app is granted permission to send SMS’s the user gets no further indication that messaging and associated costs occur

13

CQU - COIT20270 Application Development for Mobile Platforms

…Invoking Activities from BroadcastReceiver

Demonstrate and Explain how the files work

13

Sending E-Mail

The Gmail/email app lets you configure e-mail accounts using POP3 or IMAP.

You can use the built-in app or send and receive e-mails programmatically

You use an Intent object to do this. Use the setData() method on your Intent instance variable and use Uri.parse(“mailto:”) as the parameter

14

CQU - COIT20270 Application Development for Mobile Platforms

Demonstrate and Explain how the files work

14

Sending E-Mail

Then use putExtra() with parameters

Intent.EXTRA_EMAIL , array of strings containing recipients addresses

Intent.EXTRA_CC, array of strings containing other recipients addresses

Intent.EXTRA_SUBJECT, string for the subject field of e-mail

Intent.EXTRA_TEXT, string containing the message

Set the Intent message type to “message/fc822” using the Intent setType() method

Call startActivity() with Intent.createChooser (your_intent_instance, “Email’) as parameter

15

CQU - COIT20270 Application Development for Mobile Platforms

Demonstrate and Explain how the files work

15

Week 7 – Location Based Services

Objectives, to:

Display Google maps in your app, with pan and zoom control, switching between views and to add markers to the maps

Getting address of locations tapped on a map

Performing geocoding and reverse geocoding

Getting geographical data using GPS, Cell-ID and Wi-Fi

Location monitor and build a location tracker app

16

CQU - COIT20270 Application Development for Mobile Platforms

Location Based Services (LBS)

In recent times apps that take advantage of the current location of the mobile device have become popular

Use of location along with readily accessible maps allow for the integration of direction to the required service

Google maps integrates with the Android device allowing use of Google map and other services

17

CQU - COIT20270 Application Development for Mobile Platforms

Displaying Maps

Google Maps comes bundle with the Android system

The textbook is out of date – we will be using Google Maps Android API v2

For up-to-date instructions on creating the project and obtaining the Maps API key see this weeks Lecture/Tutorial

The material in chapter 9 of the Lee text will only work if you have a real Android phone (v4 or later) to test the code on, otherwise you will need to create an Android Virtual device as described in the Lecture/Tutorial. The default display now shows the Zoom control automatically

18

CQU - COIT20270 Application Development for Mobile Platforms

Changing Views

You can display Google maps in either maps view or satellite view

In some countries (US, France, UK, Australia and Canada) it is possible to display traffic conditions on the map in major cities

Green reflects smooth traffic flow, yellow moderate flow and red indicates traffic congestion

19

CQU - COIT20270 Application Development for Mobile Platforms

Navigating to a Specific Location and Adding Location Markers

You can programmatically navigate to a specific location in the world

You can also place location markers at specific points on the map

See this weeks Lecture/Tutorial for details

20

CQU - COIT20270 Application Development for Mobile Platforms

Eclipse (with Android additions) should already be installed in the labs, so these instructions are for installing on a students PC

20

Getting Touch Location

You can find the latitude and longitude of a location touched on the display

From the latitude and longitude you can find the closest address – a process known as reverse geocoding

See this weeks Lecture/Tutorial for details

21

CQU - COIT20270 Application Development for Mobile Platforms

Getting location programmatically

See this weeks Lecture/Tutorial for details

22

CQU - COIT20270 Application Development for Mobile Platforms