I don't really understand the use and concept of an Intent. I DO understand that an activity are one visual interface and one endeavour that the user can partake in. I THINK an intent is used to launch and communicate between different activities. If so, then how would you accomplish that? A code sample would be helpful. In analogy form, try to compare an intent to something in everyday life. That would help very much!
An Intent
can be used to launch activities, by supplying an action and some data. An example of using an Intent
action to view a web-page:
Intent myIntent = new Intent(Intent.VIEW_ACTION,
Uri.parse("http://www.google.com"));
Where the action is Intent.VIEW_ACTION
and the data string is an Uri
of Google's website.
Common Tasks and How To Do Them in Android
I have tried, but its tough to compare an Intent
with something in everyday life. If I come up with something, I'll jot it down with my answer.
You were right saying it was to launch and communicate between applications.
http://developer.android.com/guide/topics/fundamentals.html
That has the basics of Intents. You need to use .startActivityForResult() rather than startActivity() if you want a return value, and have a void onActivityResult(Intent intent) method to act as a Listener for when the value is returned.
The constructor takes 2 parts. The first is an int which will be a constant in the Intent class to tell the system what you want to do with the data. The second will be a URI to pass data between activities. The system then uses them to decide what App it should be passed to when you put it into the activity request. As for your analogy, I don't know, that's kind of difficult, there isn't really anything much like it. I guess the closest you're going to get is giving something to your boss and telling him you need someone else to do something with it that you can't.
To quote the API docs, an Intent
is basically a passive data structure holding an abstract description of an action to be performed, with two primary pieces of information, action and data.
At the most basic level, an Intent
can be seen as an action that you can tell Android to invoke - and what happens depends on what is registered for that action.
The action part of an Intent
is a string or string constant, and the data portion is a string representing a URI
. In addition to these main attributes, you can add new attributes via an extra, which is just a map of key-value pairs.
For more info, refer to Intents and Intent Filters, the Intent class, or Playing with Intents.
I also recommend the book Pro Android, which goes into these API details at length. There is a newer version called Pro Android 2 (haven't read it).
If you search Google Books for it, you can see extracts of the book, look at Chapter 3, "Using Resources, Content Providers, and Intents" for more info.
An intent is essentially a way of an application to declare a need. These work in conjunction with IntentFilters which basically are declarations of capabilities of what another Activity or Service can do for whomever needs it.
Intents are typically constructed of two parts a type of information and a data component which is typically a URI (Think a website, GPS data, or contact to dial).
To use an Intent you must create your intent with this type of information and data and then dispatch it to another Activity, a Service or a Broadcast Receiver which then usually binds the Intent and performs some kind of action that you requested.
The Android API details the specifics for how to build your own IntentFilters and Receivers. Look there for more info.
Assume that you n your friend a confined in two rooms having beside and without doors and having no roofs, you want give him something that he s in need. So what you do is u 'll put it on the common wall of your rooms such that he could take that thing from other side. Likely you will dump some onfo in intent(in some activity) which is global and take that info from other side(in next activity).
I find intents quite familiar, especially with some experience in application integration. Intents are basically messages, and the Android intent/activity pair is a message based architecture using asynchronous messages with both single and multi-casting, guaranteed delivery (I believe), but no guarantees on ordering.
The beauty of message based interaction is that you decouple activities from each other, both in terms of code dependencies (they need only know about a shared intent type and its payload), and in terms of their lifecycles (Android is as I understand it free to stop and resume either party in the message transaction). This makes it easier to maintain and modify activities, to reuse existing ones, and permits efficient use of resources.