Using Implicit Intents
The explicit Intent approach works fine when the activity to be started is one of yours. If you are going to start an activity from some other app, such as a Web browser to view a URL, an explicit Intent will be a problem. After all, you have no idea what Web browser will handle the request (Chrome? Firefox? Brave? Dolphin? something else?). Plus, you did not write the Web browser and do not know what classes are in it. And, even if you used tools to peek inside the other app and find out its class structure, the developers of that other app could change their implementation at any point.
Instead, you will use what are referred as the “implicit” Intent structure, which looks a lot like how the Web works.
If you have done any work on Web apps, you are aware that HTTP is based on verbs applied to URIs:
- We want to
GETthis image - We want to
POSTto this script or controller - We want to
PUTto this REST resource - Etc.
Android’s implicit Intent model works much the same way, just with a lot more verbs.
An implicit Intent is made up of two key pieces:
- A
Uriobject indicating what we want to act upon, such as aUrirepresentation of a Web site URL, and - An action string, identifying the particular action that we want
There are hundreds of action strings that are part of the Android framework, such as:
-
ACTION_VIEW, to bring up something that can view whatever theUrirefers to -
ACTION_PICK, to pick something from a collection of somethings -
ACTION_GET_CONTENT, to pick something based on a MIME type (e.g., pick an image) -
ACTION_SEND, to share some text or content with another app, often used for sending an SMS - And so on
For example, to try to view a Web page, you can use:
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://commonsware.com")))
We will see a few other Intent actions from the Android framework over the course of the rest of the book, starting with the next section. And, it is possible for apps to define their own custom actions — in that case, if the developers of those apps want you using those actions, they will need to document what those actions are and what they do.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.