Intents are essentially a message that one component uses to interact or request functionality from another component.
Apps use intents to describe an operation to perform or an event that has already occurred.
The primary pieces of information in an intent:
Action - the general action to be performed, such as viewing a PDF file, expressed as a String (ACTION_VIEW, ACTION_EDIT, etc.)
Data - the data to operate on, such as a record in a database, expressed as a Uri
There are two types of intents in Android: explicit and implicit
The intent that sends data from one activity to another within your application
Uses the class to identify the intent
The name of the class is crucial in an EXPLICIT INTENT because it specifies the exact class to be run
For example, if you are trying to get someone to date you, you EXPLICITLY say, "(insert NAME here), will you date me/be my significant other/my movie-snuggle-buddy/my romantic-restaurant-friend? "
Explicit intents are used to interact with components that reside within the same application
Especially important when starting to or binding to services
Will get an exception if you try to bind a service using an implicit intent
Pro tip: don't ask someone out by impling you are going to go to the movie and your friend quit on you. No explicit intent, no service.
Filters in the AndroidManifest.xml file are not looked at, because it is already explicitly stated which component the intent should be delivered to
All of the various methods are "chained" together.
Made possible by the set methods returning an Intent.
setSingle(false); // only if done correctly - with an explicit intent
Passing data from application to application is an implicit intent because the intent is sending the data without knowing which application or activity is going to use it.
Uses the action, data, and category elements to identify the target class
For example, research is important. Does that restaurant give you a menu? If so, find the type of DATA on the menu. If not, we need to know what the appropriate ACTION would be. If you are unsure what to do at this point, is this restaurant even in the CATEGORY of foods you like? If not, implicitly slide it into the convo that you do not intend to go.
Implicit intents are used to interact with components residing in other apps
Filters in the AndroidManifest.xml file are consulted
Intent is only delivered to the component if it "passes" through the filters
Triple pro tip: if the food does not have good reviews or pictures (and you have never been there), it does not pass.
An implicit intent may match the intent filters of multiple activities,
Luckily for us, the Android enables a "Chooser dialog" that enables the user to select the app to handle the intent, enable the app to be the default choice or be re-prompted in the future
Great example of why a Service can not start with an implicit intent because the user can not see which service starts if several service intent filters match the intent
Implicit intents help loosely connect activity to activity or activity to broadcast receivers
** Do not use implicit intents to start Android service components
There are 5 main parts of an Intent - name, action, data, category, flags, and extras. However, they can all be included in the constructor of an Intent (except for categories and flags). They are pretty straight forward and use Java Strings to describe what the name, action, data, flag, category, or extra does and is. There are also intent handlers and handlers to handle the intent handlers.
Specified declaratively in AndroidManifest.xml file (see Other for the purpose of the xml file :))
Specified programmatically in Java code (see AP Computer Science A , COMP SCI 200 , COMP SCI 300 for more information on Java)
There are multiple ways to pass things to activities with intents.
Action - using a string constant value that is defined in the Intent class
Data object of the Intent - using an URI (uniform resource identifier)
Intent extras - using a key-value pair, where the key is a string and the value is a primitive type
The filters are set up inside the application manifest file (AndroidManifest.xml)
By setting the action of the intent-filter, the application is ready to handle whatever action another application on the device sends to it
By setting the category of the intent-filter, the application limits what actions get processed
Inside the AndroidManifest file, the following aspects of the intent-filter can be set: