Explicit Intent and implicit Intent functions and Usage Analysis ., Intent usage
Functions and usage of explicit Intent and implicit Intent.
Intent, which means Intent in Chinese. Is what you want to do.
If you use startActivity (Intentintent), startActivityForResult (Intentintent), or other methods to use it, it meansDo what you want. (Do what you want to do)
First, let's take a general look at their usage differences:
1. Explicit Intent (Explicit Intent): it is mainly used to call the components of its own application (activity, service), or the components that others explicitly want to call. (This is rarely used because it is rarely called in this way)
2. Implicit Intent (Implicit Intent): Mainly used to start programs other than this program.
Then let's talk about their respective usage methods.
I. ExplicitIntent:
Should I have an Intent before use? Let's first create one.
Creation method. Intent intent = newIntent (Context context, Class <?> Class );
The class parameter that follows is a reflection of a class.
You can also remember the class name. class of the class where the target Activity is located;
Usage:
StartActivity (intent) or startActivityForResult (intent );
Then you can.
2. ImplicitIntent
We use two roles for parsing.
First, the caller identity. (To jump to the current application of another application)
The main method for creating an implicit Intent is as follows:
Initial Action with Intent (String action)
Empty Intent: Intent ();
Initially, both Action and Uri are included: Intent (String action, Uri uri );
In fact, the usage of these three methods is not much different, but the constructor with more initial parameters is sometimes easier.
You can also set or change the parameter value after building Intent.
For example, intent. setAction (String action) and Uri settings: setData (Uri uri), MIME type: setType (), etc.
Note that if you need to set both the URI and MIME types, use the setDataAndType () method, because when you call setData and setType respectively, they will invalidate one of them. Why? In fact, there are Type settings in the URI, which will conflict and overwrite the other settings.
Other attributes are also critical. We recommend that you read the Android development documentation. I personally say that it is better for you to find and explore on your own.
The implicit Intent call is also the same as the explicit call.
When you call Intent implicitly, if you want to start multiple programs with certain functions, for example, when you use the program to send files, there are many programs for you to choose? For example, Bluetooth or QQ. If there are multiple programs that can implement a function, a list will appear for you to select. Only when a single program is started.
Generally, you do not know what programs to start, but the system starts it yourself. Is this a poor user experience? In this case, you can implement the function of selecting a box even if only one program is available: Chooser.
The Chooser itself is also an Intent, but it is an Intent with a selector attached.
Example:
Intent intent = new Intent ();
...
Intent chooser = intent. createChooser (intent, title );
The title here is the title string of the Selection box.
2. Receive implicit Intent
If you want your program to accept the implicit Intent from other programs, what should you do?
Declare in the Manifest file that you can receive a certain type of implicit Intent.
For example:
<Activityandroid: name = "activity">
<Intent-filter>
<Action android: name = "android. intent. action. SEND"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Data android: mimeType = "text/plain"/>
</Intent-filter>
</Activity>
First, parse the Intent-filte Chinese translation. Intent (Intent) filter.
SoWhy is this filter available??
First, you can think about it. If there is no filter, the system cannot identify which programs can use when you want to start a program with a certain function, all references are listed. Isn't that good?
By default, if no Intent filter (Intent-filter) exists in an application, nothing can pass its review.
You can think of an application as a water dispenser. The intent filter is used to filter water. So when a water dispenser (Application) does not have a filter, would I rather make it fail to pass the review to others? Those that have not been filtered are prone to many harmful substances. Do you dare to drink anything you have never filtered ?.
When we have a filter, we can rest assured that we can drink the water filtered by the filter (equivalent to using the application that has passed the review ).
Then, I have another question.
How to filter intent?
The answer is, of course, a layer-by-layer filtering.
The data in the figure is URI and MIME type.
As long as there is a path that can pass through, it is passed, and some specific functions of the program can be used.
For more details about Intent, see the official android documentation.