0% found this document useful (0 votes)
19 views

UNIT V Android Programming

The document discusses how to programmatically send and receive SMS messages and emails on Android. It also covers displaying Google Maps within an Android application, adding markers, and getting location data. Key classes and methods discussed include SmsManager, BroadcastReceiver, Intent, and MapView.

Uploaded by

toyabshah0895
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

UNIT V Android Programming

The document discusses how to programmatically send and receive SMS messages and emails on Android. It also covers displaying Google Maps within an Android application, adding markers, and getting location data. Key classes and methods discussed include SmsManager, BroadcastReceiver, Intent, and MapView.

Uploaded by

toyabshah0895
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

UNIT V

Android Programming
Using Basic Views

● Messaging and E-mail:


● SMS Messaging:
○ Sending SMS Messages Programmatically, Getting Feedback after Sending a
Message,
○ Sending SMS Messages Using Intent, Receiving SMS Messages
● Sending E-mail :
○ Using SmsManager to send SMS,
○ Using Built-in Intent to send SMS
● Google Maps:
○ Location-Based Services and Google Map, Display Google Maps, Creating the
project, Obtaining the Maps API Key, Displaying the Map, Displaying the
○ Zoom Control, Changing Views, Navigating to a specific location, Adding
Markers, Getting the location that was touched, Geocoding and Reverse
Geocoding Getting Location Data , Monitoring a Location
SMS Messaging

● SMS messaging is one of the main killer applications on a mobile phone today.
● Any mobile phone you buy today should have at least SMS messaging capabilities, and
nearly all users of any age know how to send and receive such messages.
● Android comes with a built-in SMS application that enables you to send and receive SMS
messages.
● However, in some cases you might want to integrate SMS capabilities into your own Android
application.
● For example, you might want to write an application that automatically sends a SMS
message at regular time intervals.
● This section describes how you can programmatically send and receive SMS messages in
your Android applications.
SMS Messaging

● Sending SMS Messages Programmatically


● In the AndroidManifest.xml file

<uses-permission android:name=”android.permission.SEND_SMS”></uses-permission>

In Code

private void sendSMS(String phoneNumber, String message)

SmsManager sms = SmsManager.getDefault();

sms.sendTextMessage(phoneNumber, null, message, null, null);

}
SMS Messaging

● Android uses a permissions-based policy whereby all the permissions needed by an


application must be specified in the AndroidManifest.xml file.
● This ensures that when the application is installed, the user knows exactly which access
permissions it requires.
● Because sending SMS messages incurs additional costs on the user’s end, indicating the
SMS permissions in the AndroidManifest.xml file enables users to decide whether to allow
the application to install or not.
● To send an SMS message programmatically, you use the SmsManager class.
● Unlike other classes, you do not directly instantiate this class; instead, you call the
getDefault() static method to obtain a SmsManager object.
● Then send the SMS message using the sendTextMessage() method:
SMS Messaging

● Following are the five arguments to the sendTextMessage() method:

sms.sendTextMessage(phoneNumber, null, message, null, null);

● destinationAddress ➤➤ — Phone number of the recipient


● scAddress ➤➤ — Service center address; use null for default SMSC
● text ➤➤ — Content of the SMS message
● sentIntent ➤➤ — Pending intent to invoke when the message is sent
● deliveryIntent ➤➤ — Pending intent to invoke when the message has been delivered
SMS Messaging

● Getting Feedback after Sending the Message


● In the previous section, we learned how to programmatically send SMS messages using the
● SmsManager class; but how do you know that the message has been sent correctly?
● To do so, create two PendingIntent objects to monitor the status of the SMS
message-sending process.
● These two Pending Intent objects are passed to the last two arguments of the
sendTextMessage() method
SMS Messaging

● String SENT = “SMS_SENT”;


● String DELIVERED = “SMS_DELIVERED”;
● PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
● PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

SMS Messaging


SMS Messaging


SMS Messaging

● Created two PendingIntent objects.


● Then registered for two BroadcastReceivers.
● These two BroadcastReceivers listen for intents that match “SMS_SENT” and
“SMS_DELIVERED”
● (which are fired by the OS when the message has been sent and delivered, respectively).
● Within each BroadcastReceiver you override the onReceive() method and get the current
result code.
● The two PendingIntent objects are passed into the last two arguments of the
sendTextMessage() method:

sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

● In this case, whether a message has been sent correctly or failed to be delivered, you will be
notified of its status via the two PendingIntent objects.
SMS Messaging

Sending SMS Messages Using Intent

Using the SmsManager class, you can send SMS messages from within your application
without the need to involve the built-in Messaging application.

However, sometimes it would be easier if you could simply invoke the built-in Messaging
application and let it do all the work of sending the message.

To activate the built-in Messaging application from within your application, you can use an
Intent object together with the MIME type “vnd.android-dir/mms-sms”
SMS Messaging
SMS Messaging

Receiving SMS messages

Besides sending SMS messages from your Android applications, you can also receive
incoming SMS messages from within your application by using a BroadcastReceiver object.

This is useful when you want your application to perform an action when a certain SMS
message is received.

In Android Manifest

<uses-permission android:name=”android.permission.RECEIVE_SMS”>

</uses-permission>
SMS Messaging
SMS Messaging

● To listen for incoming SMS messages, you create a BroadcastReceiver class.


● The BroadcastReceiver class enables your application to receive intents sent by other
applications using the sendBroadcast() method.
● Essentially, it enables your application to handle events raised by other applications. When
an intent is received, the onReceive() method is called; hence, you need to override this.
● When an incoming SMS message is received, the onReceive() method is fired.
● The SMS message is contained in the Intent object (intent; the second parameter in the
onReceive() method) via a Bundle object.
● The messages are stored in an Object array in the PDU format.
● To extract each message, you use the static createFromPdu() method from the
SmsMessage class.
● The phone number of the sender is obtained via the getOriginatingAddress() method, so if
you need to send an autoreply to the sender, this is the method to obtain the sender’s phone
number.
Sending E‑Mail

● Like SMS messaging, Android also supports e-mail. The Gmail/Email application on Android
enables you to configure an e-mail account using POP3 or IMAP.
● Besides sending and receiving e-mails using the Gmail/Email application, you can also send
e-mail messages programmatically from within your Android application.

Sending E‑Mail

In this example, you are launching the built-in Email application to send an e-mail message.
To do so, you use an Intent object and set the various parameters using the setData(),
putExtra(), and setType() methods:
Google Maps

Displaying Maps

● Google Maps is one of the many applications bundled with the Android platform.
● In addition to simply using the Maps application, you can also embed it into your own
applications and make it do some very cool things.
● This section describes how to use Google Maps in your Android applications and
programmatically perform the following:
● Change the views of Google Maps.
● Obtain the latitude and longitude of locations in Google Maps.
● Perform geocoding and reverse geocoding (translating an address to latitude and
longitude
● and vice versa).
● Add markers to Google Maps.
Google Maps

Obtaining the maps Api key

● you need to apply for a free Google Maps API key before you can integrate Google Maps
into your Android application.
● When you apply for the key, you must also agree to Google’s terms of use, so be sure to read
them carefully.
Google Maps

Displaying the map

● You are now ready to display Google Maps in your Android application. This involves two
main tasks:
○ Modify your AndroidManifest.xml file by adding both the <uses-library> element
and the INTERNET permission.
○ Add the MapView element to your UI.
Google Maps
Google Maps
Google Maps

Note that MainActivity is now extending the MapActivity class


Google Maps
Google Maps

● In order to display Google Maps in your application, you first need to have the INTERNET
permission in your manifest file.
● You then add the <com.google.android.maps.MapView> element to your UI file to embed
the map within your activity.
● Very importantly, your activity must now extend the MapActivity class, which itself is an
extension of the Activity class.
● For the MapActivity class, you need to implement one method: isRouteDisplayed().
● This method is used for Google’s accounting purposes, and you should return true for this
method if you are displaying routing information on the map. For most simple cases, you
can simply return false.

Google Maps

The previous section showed how you can display Google Maps in your Android application.

You can pan the map to any desired location and it will be updated on-the-fly.

However, on the emulator there is no way to zoom in or out from a particular location (on a
real Android device you can pinch the map to zoom it).

Thus, in this section, you will learn how you can let users zoom in or out of the map using
the built-in zoom controls.
Google Maps
Google Maps

To display the built-in zoom controls, you first get a reference to the map and then call the
setBuiltInZoomControls() method:

mapView = (MapView) findViewById(R.id.mapView);

mapView.setBuiltInZoomControls(true);

Besides displaying the zoom controls, you can also programmatically zoom in or out of the
map using the zoomIn() or zoomOut() method of the MapController class.

The following Try It Out shows you how to achieve this.


Google Maps
Google Maps

Changing Views

Google Maps is displayed in map view, which is basically drawings of streets and places

of interest.

You can also set Google Maps to display in satellite view using the setSatellite() method of
the MapView class:
Google Maps

● Besides satellite view, you can also display the map in street view (which highlights all the
streets on the map) using the setStreetView() method

● If you want to display traffic conditions on the map, use the setTraffic() method:
● mapView.setTraffic(true);
Google Maps

Navigating to a Specific Location

By default, Google Maps displays the map of the United States when it is first loaded.
However, you can also set Google Maps to display a particular location.

In this case, you can use the animateTo() method of the MapController class
Google Maps
Google Maps

● In the preceding code, you first obtain a map controller from the MapView instance and
assign it to a MapController object (mc).
● You then use a GeoPoint object to represent a geographical location.
● Note that for this class, the latitude and longitude of a location are represented in micro
degrees.
● that they are stored as integer values. For a latitude value of 40.747778, for example, you
need to multiply it by 1e6 (which is one million) to obtain 40747778.
● To navigate the map to a particular location, you can use the animateTo() method of the
MapController class.
● The setZoom() method enables you to specify the zoom level at which the map is displayed
(the bigger the number, the more details you see on the map).
● The invalidate() method forces the MapView to be redrawn.
Google Maps

Adding Markers
Adding markers to a map to indicate places of interest enables your users to easily locate the
places they are looking for
To add a marker to the map, you first need to define a class that extends the Overlay class:
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when)
{
//...
}
}
Google Maps

An overlay represents an individual item that you can draw on the map. You can add as
many overlays

as you want. In the MapOverlay class, override the draw() method so that you can draw the
pushpin image on the map. In particular, note that you need to translate the geographical
location (represented by a GeoPoint object, p) into screen coordinates:

//---translate the GeoPoint to screen pixels---

Point screenPts = new Point();

mapView.getProjection().toPixels(p, screenPts);
Google Maps

Because you want the pointed tip of the pushpin to indicate the position of the location, you
need to deduct the height of the image (which is 50 pixels) from the y coordinate of the
point and draw the image at that location

//---add the marker---

Bitmap bmp = BitmapFactory.decodeResource(

getResources(), R.drawable.pushpin);

canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);


Google Maps

To add the marker, create an instance of the MapOverlay class and add it to the list of
overlays available on the MapView object:

//---Add a location marker---

MapOverlay mapOverlay = new MapOverlay();

List<Overlay> listOfOverlays = mapView.getOverlays();

listOfOverlays.clear();

listOfOverlays.add(mapOverlay
Google Maps

Getting the Location That Was Touched


After using Google Maps for a while, you may want to know the latitude and longitude of a
location corresponding to the position on the screen that was just touched. Knowing this
information is very useful, as you can determine a location’s address, a process known as
reverse geocoding
If you have added an overlay to the map, you can override the onTouchEvent() method
within the MapOverlay class.
This method is fired every time the user touches the map. This method has two parameters:
MotionEvent and MapView.
Using the MotionEvent parameter, you can determine whether the user has lifted his or her
finger from the screen using the getAction() method.
In the following code snippet, if the user has touched and then lifted the finger, you display
the latitude and longitude of the location touched:
Google Maps
Google Maps

The getProjection() method returns a projection for converting between screen-pixel


coordinates and latitude/longitude coordinates.

The fromPixels() method then converts the screen coordinates into a GeoPoint object.
Google Maps

Monitoring a Location

One very cool feature of the LocationManager class is its ability to monitor a specific location.

This is achieved using the addProximityAlert() method.

The following code snippet shows how to monitor a particular location so that if the user is within a five-meter radius
from that location, your application will fire an intent to launch the web browser:

//---use the LocationManager class to obtain locations data---

lm = (LocationManager)

getSystemService(Context.LOCATION_SERVICE);

//---PendingIntent to launch activity if the user is within some locations---

PendingIntent pendIntent = PendingIntent.getActivity( this, 0, new Intent(android.content.Intent.ACTION_VIEW,

Uri.parse(“http://www.amazon.com”)), 0);

lm.addProximityAlert(37.422006, -122.084095, 5, -1, pendIntent);


Google Maps

The addProximityAlert() method takes five arguments: latitude, longitude, radius (in meters),
expiration (time for the proximity alert to be valid, after which it will be deleted; -1 for no
expiration), and the pending intent.

You might also like