14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: Intents, Broadcast Receivers, Adapters, and the Internet<br />

Creating an Earthquake Viewer<br />

148<br />

In the following example, you’ll create a tool that uses a USGS earthquake feed to display a list of recent<br />

earthquakes.<br />

You will return to this Earthquake application several times, fi rst in Chapter 6 to save and share the<br />

earthquake data with a Content Provider, and again in Chapters 7 and 8 to add mapping support and to<br />

move the earthquake updates into a background Service.<br />

In this example, you will create a list-based Activity that connects to an earthquake feed and displays<br />

the location, magnitude, and time of the earthquakes it contains. You’ll use an Alert Dialog to provide a<br />

detail window that includes a linkifi ed Text View with a link to the USGS web site.<br />

1. Start by creating an Earthquake project featuring an Earthquake Activity. Modify the main.xml<br />

layout resource to include a List View control — be sure to name it so you can reference it from<br />

the Activity code.<br />

<br />

<br />

<br />

<br />

2. Create a new public Quake class. This class will be used to store the details (date, details, location,<br />

magnitude, and link) of each earthquake. Override the toString method to provide the<br />

string that will be used for each quake in the List View.<br />

package com.paad.earthquake;<br />

import java.util.Date;<br />

import java.text.SimpleDateFormat;<br />

import android.location.Location;<br />

public class Quake {<br />

private Date date;<br />

private String details;<br />

private Location location;<br />

private double magnitude;<br />

private String link;<br />

public Date getDate() { return date; }<br />

public String getDetails() { return details; }<br />

public Location getLocation() { return location; }<br />

public double getMagnitude() { return magnitude; }<br />

public String getLink() { return link; }<br />

public Quake(Date _d, String _det, Location _loc, double _mag,<br />

String _link) {<br />

date = _d;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!