UNIT V Android Programming
UNIT V Android Programming
Android Programming
Using Basic Views
● 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
<uses-permission android:name=”android.permission.SEND_SMS”></uses-permission>
In Code
}
SMS Messaging
●
SMS Messaging
●
SMS Messaging
● 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
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
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
● 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
● 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
● 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
● 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.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.
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
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:
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
getResources(), R.drawable.pushpin);
To add the marker, create an instance of the MapOverlay class and add it to the list of
overlays available on the MapView object:
listOfOverlays.clear();
listOfOverlays.add(mapOverlay
Google Maps
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.
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:
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Uri.parse(“http://www.amazon.com”)), 0);
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.