What is a Intent ?
Intent is basically a message that is passed between components (such as Activities, Services, Broadcast Receivers, and Content Providers).
So, it is almost equivalent to parameters passed to API calls. The
fundamental differences between API calls and intents’ way of invoking
components are:
- API calls are synchronous while intent-based invocations are asynchronous.
- API calls are compile time binding while intent-based calls are run-time binding.
Of course, Intents can be made to work exactly like API calls by using what are called explicit intents, which will be explained later. But more often than not, implicit intents are the way to go and that is what is explained here.
One component that wants to invoke another has to only express its’ intent to do a job. And any other component that exists and has claimed that it can do such a job through intent-filters,
is invoked by the android platform to accomplish the job. This means,
both the components are not aware of each other’s existence and can
still work together to give the desired result for the end-user.
This invisible connection between components is achieved through the
combination of intents, intent-filters and the android platform.
This leads to huge possibilities like:
- Mix and match or rather plug and play of components at runtime.
- Replacing the inbuilt android applications with custom developed applications.
- Component level reuse within and across applications.
- Service orientation to the most granular level, if I may say.
Here is additional description about intent, almost formal.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch anActivity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent)or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding
between the code in different applications. Its most significant use is
in the launching of activities, where it can be thought of as the glue
between activities. It is basically a passive data structure holding an
abstract description of an action to be performed. The primary pieces of
information in an intent are:
- action
The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc. - data
The data to operate on, such as a person record in the contacts database, expressed as a Uri.
All Android components that wish to be notified via intents should
declare intent filters so that Android knows which intents should go to
that component. So, we need to add intent-filter elements to our AndroidManifest.xml file. It looks something like this:
<?xml version=”1.0″ encoding=”utf-8″?><manifest xmlns:android=“http://schemas.android.com/apk/res/android”package=”com.bogotobogo.myContacts”android:versionCode=”1″android:versionName=”1.0″><application android:icon=”@drawable/icon”android:label=”@string/app_name”><activity android:name=”.myContacts”android:label=”@string/app_name”><intent-filter><action android:name=“android.intent.action.MAIN” /><categoryandroid:name=”android.intent.category.LAUNCHER” /></intent-filter></activity></application><uses-sdk android:minSdkVersion=”4″ /><uses-permission android:name=“android.permission.READ_CONTACTS”/></manifest>
Note that intent-filter element is under the activity element.
In the file, we are declaring that this activity is (1) the main
activity for this application and (2) the activity is in the LAUNCHER
category, meaning it gets an icon in the Android menu. Because this
activity is the main one for the application, Android knows this is the
component it should launch when someone chooses the application from the
main menu.
Once we have our intent, we need to pass it to Android and get the child activity to launch. Here, we have two options:
- Call startActivity() with the Intent. This will cause Android to find the best matching activity and pass the intent to the activity for handling. The activity will not be informed when the child activity is complete.
- Call startActivityForResult(), passing it the intent and a number which is unique to the calling activity. Android will find the best matching activity and pass the intent over to the activity. The activity will be notified when the child activity is complete via onActivityResult() callback.
No comments:
Post a Comment