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!
Android Intent Tutorial. Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc. The dictionary meaning of intent is intention or purpose.
An Intent object is a bundle of information which is used by the component that receives the intent as well as information used by the Android system. An Intent object can contain the following components based on what it is communicating or going to perform −
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With