Activity, service, and broadcast receiver are activated through messages. The message is an intent.
Intent messages can be used for communication between the current running time and components in the application or between components of different applications. Intent itself is an intent object, which contains the abstract data structure of an execution operation, which is passed to the component that executes the operation, or, in the case of broadcast, this data structure is used to describe what is being executed or has occurred.
The intent sending mechanism varies depending on the component type:
- For activity, the context. startactivity () method transmits intent, starts a new activity, or the activity. startactivityforresult () method starts the new activity and returns it to the activity after it completes the task;
- For service, context. startservice () method, used to create a service or pass a command to a running service. Similarly, context. bindservice () is used to establish a connection between the current component and the service. Optional. if the service is not running, you can create a new instance;
- For the broadcast receiver, you can use the context. sendbroadcast (), context. sendorderedbroadcast (), or context. sendstickybroadcast () Methods to send intent to all interested broadcast receivers.
In the preceding circumstances, the android system finds the activity, service, or broadcast service set that is suitable for responding to the intent. There will be no overlap, that is, broadcast intent will only send to broadcast receiver, rather than activity or service.
The intent object can be bound to a group of information:
- Receive the information required by the intent component. For example, you need to call the System camera activity to inform it of the path to store the photo;
- The information required by the Android system. For example, a component of that type can process this intent. For example, it tells you how to start the activity (for example, in which task it is required ).
Intent objects mainly include the following:
- Componnet name, component name. The name of the intent component that can be processed. The component name is optional. If you enter a component name, the intent object will be sent to the component with the specified component name. Otherwise, you can locate the appropriate component through other intent information. The component name is a componentname type object, which includes:
- Complete class name of the target component, such as com. example. Project. App. freneticactivity;
- The package name set in the manifest file, the package name of the component, and the package name defined in the manifest file may not match.
- Action: The string of the action. It is used to execute an action. Alternatively, it indicates an action that has occurred or is to be reported when broadcast. The intent class defines a group of Action constants. For details, see the intent. Action _ * API _*. These are some general actions premade by the intent class, and some actions are defined in other android APIs. You can define your own action string constants to activate your application components. This requires that the package name of the application be used as the prefix of the string, for example, Com. example. Project. show_color. The action name is similar to the method name called by Java. The data and extra parameters and return values mentioned below.
- Data to indicate the data and data MIME types. Different actions are compatible with different data types. For example, if action is action_edit, data must contain the document URI to be edited. If action is action_call, data may be Tel: prefix followed by phone number. For example, action is action_view and data is the URI starting with HTTP, the URI is displayed or downloaded. In the process of matching intent to processing this component, the data (MIME type) type is very important. For example, a component can display image data but cannot play audio files. In many cases, the data type can be found in the URI. For example, the URI starting with content: indicates that the data is on the device and has the content
Provider control. However, some types can only be explicitly set. The setdata () method can only set the data Uri, settype () can set the MIME type, setdataandtype () can set the URI or set the MIME type.
- Category, which contains additional information about the intent component type. You can set any number of category descriptions for an intent. Similar to cation, the intent class prefabricated some category constants, intent. Category _*.
- Extras can be seen as a map. Through key-value pairs, some additional information can be provided for processing intent components. You can use put... () and get... () to access information. You can also obtain the bundle object and then access it through the putextras () and getextras () methods.
- Flug is used in many cases. flug is added to intent. For example, it can indicate how Android starts an activity, such as whether the activity belongs to or does not belong to the current task, and the ownership of the activity after processing. These flags are defined in constants of the intent class.
Intent parses the delivery of intent in two ways:
- Explicitly set the component name of the target component. However, sometimes developers do not know the component name of other applications. Explicit mode is often used for message transmission within the application. For example, an activity in the application starts a related service or a sister activity;
- When the implicit intent and component names are empty. This method is often used to activate components in other applications.
Android delivers an explicit intent. You only need to find the component with the corresponding name.
Implicit intent requires different policies. Android needs to find the most appropriate component (SET) to process this intent ). Use intent filter to compare the association structure between intent objects and components. Filters determine the intent they can process based on their capabilities. The Android system opens a suitable component to process the corresponding implicit intent. If the component does not contain any intent filter, it can only receive the explicit intent. The component with filter can receive both implicit intent and explicit.
Intent has three aspects for intent filter:
- Action
- Data, including Uri and Data Type
- Category
Extra and flag do not work in this regard.
Intent filter in order to support implicit intent, activity, service, and broadcast receiver will contain one or more intent filters. Each intent filter describes the ability of a component to receive a set of intents. In intent filter, the acceptable type and the intent you do not want are described. To deliver an implicit intent to a component, you only need to use a filter of the component.
The filter component divides the filter into multiple filters to target different tasks. In the note pad example in the sample, the noteeditor activity has two filters, one for starting and opening the specified note, and the other for opening the new blank note.
An intent filter is an instance of the intentfilter class. However, the android system must know its capabilities when the component is not started. Therefore, intent filter is generally not set in Java code, it is declared as the <intent-filter> element in the manifest file of the application. One exception is that a dynamic filter is registered for the broadcast receiver. You can call the context. registerreceiver () method to create an intentfilter object by directly instantiating it.
Filter has three equal parts: Action, data, and category. Implicit intent will test the three parts. To deliver an intent to a component, all three tests must pass. Of course, if the component has multiple intent filters, one intent may fail, but another one is passed, so that the intent can be delivered to the component.
Action test can contain multiple actions in intent filter, for example:
<Intent-filter...>
<Action Android: Name = "com. example. Project. show_current"/>
<Action Android: Name = "com. example. Project. show_recent"/>
<Action Android: Name = "com. example. Project. show_pending"/>
...
</Intent-filter>
To pass the test, the action name in intent must match one of them.
If the intent filter does not contain the action list, but the intent specifies the action, the intent does not match the action and does not pass the test. The intent filter does not specify the action, but the intent filter does.
The category test can contain the category list in the intent filter:
<Intent-filter...>
<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "android. Intent. Category. browsable"/>
...
</Intent-filter>
To pass the test, intent must match the category in an intent filter.
In principle, if intent has no category settings, it can always pass the test. This is basically correct, but there is an exception. When Android executes the startactivity () method for all implicit intents, it considers that they contain at least one android. Intent. Category. default. Therefore, to receive an implicit intent, the activity must add this category.
The data element of the date test can be repeated multiple times in the intent filter element (the Action and category cannot be repeated) or not at all. For example:
<Intent-filter...>
<Data Android: mimetype = "Video/MPEG" Android: Scheme = "HTTP".../>
<Data Android: mimetype = "audio/MPEG" Android: Scheme = "HTTP".../>
...
</Intent-filter>
Specify the URI and data type (MIME type) in the data element ). Uri is represented separately:
Scheme: // host: Port/path
The host and port are associated. If the host is not set, the port is ignored.
All these attributes are optional but not independent. For example, if you want to set path, you must also set schema, host, and port.
When comparing the URI specified in the intent filter and the URI specified in the intent filter, only the URL section mentioned in the intent filter is compared. For example, if the intent filter only mentions schema, all URLs that contain this schema match. In the path section of the filter, you can use wildcards for flexible matching.
The mimetype attribute is more commonly used than the URI method. Both intent and intent filters can use MIME wildcards, such as text /*.
If both mimetype and Uri exist, the comparison rules are as follows:
- If neither intent nor intent filter sets any URI or mimetype;
- If the intent contains a URI but does not have a data type, the URI part of the intent filter matches the URI part, and there is no data type part. You can use it, such as mailto: And Tel:
- The intent object contains the data type but does not have the URI part. If the intent filter only has the data type but does not have the URI part, the data can be passed;
- The intent object includes the URI and data type (or the data type is in URI). The test is divided into two parts. The data type of the intent object must match the intent filter and the URI of the intent object, or match the URI in the intent filter, or the intent filter does not contain the URI part (only when the URI of the intent object is content: or file ).
|