Intents

Intents are used as a message-passing mechanism that works both within your application, and between
applications. Intents can be used to:

- Declare your intention that an Activity or Service be started to perform an action, usually with (or on) a particular piece of data.
- Broadcast that an event (or action) has occurred.
- Explicitly start a particular Service or Activity.

You can use Intents to support interaction among any of the application components installed on an Android device, no matter which application they’re a part of. This turns your device from a platform containing a collection of independent components into a single interconnected system.

One of the most common uses for Intents is to start new Activities, either explicitly (by specifying the class to load) or implicitly (by requesting that an action be performed on a piece of data).
 
Intent intent = new Intent(MyActivity.this, MyOtherActivity.class);
startActivity(intent);


Starting an Activity for a result
private static final int SHOW_SUBACTIVITY = 1;
Intent intent = new Intent(this, MyOtherActivity.class);
startActivityForResult(intent, SHOW_SUBACTIVITY);

Intents can also be used to broadcast messages across the system. Any application can register Broadcast Receivers to listen for, and react to, these broadcast Intents. This lets you create event-driven applications based on internal, system, or third-party-application events.

Android broadcasts Intents to announce system events, like changes in Internet connection status or battery charge levels. The native Android applications, such as the phone dialer and SMS manager, simply register components that listen for specific broadcast Intents — such as ‘‘incoming phone call’’ or ‘‘SMS message received’’ — and react accordingly.

Using Intents to propagate actions — even within the same application — is a fundamental Android design principle. It encourages the decoupling of components, to allow the seamless replacement of application elements. It also provides the basis of a simple model for extending an application’s functionality.

No comments:

Post a Comment