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 />

114<br />

An earthquake-monitoring example will then demonstrate how to tie all these features together. The<br />

earthquake monitor will form the basis of an ongoing example that you’ll improve and extend in later<br />

chapters.<br />

Introducing Intents<br />

Intents are used as a message-passing mechanism that lets you declare your intention that an action be<br />

performed, usually with (or on) a particular piece of data.<br />

You can use Intents to support interaction between any of the application components available on an<br />

Android device, no matter which application they’re part of. This turns a collection of independent<br />

components into a single, interconnected system.<br />

One of the most common uses for Intents is to start new Activities, either explicitly (by specifying the<br />

class to load) or implicitly (by requesting an action be performed on a piece of data).<br />

Intents can also be used to broadcast messages across the system. Any application can register a Broadcast<br />

Receiver to listen for, and react to, these broadcast Intents. This lets you create event-driven applications<br />

based on internal, system, or third-party application events.<br />

Android uses broadcast Intents to announce system events, like changes in Internet connection status<br />

or battery charge levels. The native Android applications, such as the phone dialer and SMS manager,<br />

simply register components that listen for specifi c broadcast Intents — such as “incoming phone call” or<br />

“SMS message received” — and react accordingly.<br />

Using Intents to propagate actions — even within the same application — is a fundamental Android<br />

design principle. It encourages the decoupling of components, to allow the seamless replacement of<br />

application elements. It also provides the basis of a simple model for extending functionality.<br />

Using Intents to Launch Activities<br />

The most common use of Intents is to bind your application components. Intents are used to start, stop,<br />

and transition between the Activities within an application.<br />

The instructions given in this section refer to starting new Activities, but the same rules generally<br />

apply to Services as well. Details on starting (and creating) Services are available in Chapter 8.<br />

To open a different application screen (Activity) in your application, call startActivity, passing in an<br />

Intent, as shown in the snippet below.<br />

startActivity(myIntent);<br />

The Intent can either explicitly specify the class to open, or include an action that the target should<br />

perform. In the latter case, the run time will choose the Activity to open, using a process known as<br />

“Intent resolution.”<br />

The startActivity method fi nds, and starts, the single Activity that best matches your Intent.

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

Saved successfully!

Ooh no, something went wrong!