Intent
The basic concept
An Intent is a vehicle for passing information between objects. For example, when one Ability needs to start another Ability, or an AbilitySlice needs to navigate to another AbilitySlice, the Intent can be used to specify the target to start with data. Intent elements include Operation and Parameters, described in Table 1. Table 1 Components of IntEnts
attribute | Child attributes | describe |
---|---|---|
Operation | Action | Indicates an Action. A preset Action is usually used. An application can also customize an Action. For example, IntentConstants.ACTION_HOME indicates a return to the desktop action. |
Entity | Indicates a category. The Entity preset by the system is usually used, and the application can also customize the Entity. For example, intent.entity_HOME indicates that the icon is displayed on the desktop. | |
Uri | Represents a Uri description. If a Uri is specified in the Intent, the Intent matches the specified Uri information, including Scheme, schemeSpecificPart, Authority, and PATH. | |
Flags | Represents how an Intent is handled. For example, the intent.flag_ability_CONTINUATION flag indicates whether a local Ability can be migrated to a remote device to continue running. | |
BundleName | Represents the package description. If both BundleName and AbilityName are specified in an Intent, the Intent can directly match the specified Ability. | |
AbilityName | Represents the name of the Ability to be enabled. If both BundleName and AbilityName are specified in an Intent, the Intent can directly match the specified Ability. | |
DeviceId | Represents the device ID that runs the specified Ability. | |
Parameters | – | Parameters are customizable data structures that allow developers to pass additional information needed for certain requests. |
When an Intent is used to initiate a request, it is of two types, depending on the specified element:
- If both BundleName and AbilityName are specified, the application is launched directly based on the full name of Ability (e.g. “com.demoapp.fooability”).
- If both BundleName and AbilityName are not specified, the application is started based on the other properties in Operation.
Note When an Intent sets an attribute, it must first set the attribute using Operation. To add or modify attributes, you must set Operation before performing the Operation.
For the easiest way to use IntEnts, see quick Start sample code. “Implementing a page jump” describes how to use Intent to realize the jump relationship between two pages.
Launch the app according to the full name of Ability
You can start a Ability and navigate to it by constructing an Operation object that contains BundleName and AbilityName. Example code is as follows:
Intent intent = new Intent();
// Create an operation object using the OperationBuilder class in the Intent, specifying the device identifier (empty string to indicate the current device), the application package name, and the Ability name
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.demoapp")
.withAbilityName("com.demoapp.FooAbility")
.build();
// Set operation to the intent
intent.setOperation(operation);
startAbility(intent);
Copy the code
As the object handling the request, the Intent object passed by the requester is received in the corresponding callback method. In the case of navigation to another Ability, the navigation target Ability can get the Intent object as an argument to its onStart() callback. Start an application based on other properties of Operation. In some cases, developers need to use a capability provided by another application in an application without being aware of which application provides that capability. For example, if a developer needs to open a link through a browser and does not care which browser application the user ultimately chooses, the required capabilities can be described through Operation properties other than BundleName and AbilityName. If multiple applications on the device provide the same capability, the system displays a candidate list and allows users to select the application to process requests. The following example shows using Intent to query weather information across Ability.
The requester
Construct the Intent in Ability along with the Operation object containing the Action and call the startAbilityForResult() method to initiate the request. The onAbilityResult() callback method is then overridden to process the request result.
private void queryWeather(a) {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withAction(Intent.ACTION_QUERY_WEATHER)
.build();
intent.setOperation(operation);
startAbilityForResult(intent, REQ_CODE_QUERY_WEATHER);
}
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
switch (requestCode) {
case REQ_CODE_QUERY_WEATHER:
// Do something with result..return;
default:... }}Copy the code
Handler 1. As the object of processing the request, you first need to declare the externally provided capabilities in the configuration file so that the system can identify itself as a candidate for the request handler.
{
"module": {..."abilities": [{..."skills":[
{
"actions": ["ability.intent.QUERY_WEATHER"]}]... }]... }... }Copy the code
2. Configure the route in Ability to enable navigation to the corresponding AbilitySlice from this action.
@Override
protected void onStart(Intent intent) {... addActionRoute(Intent.ACTION_QUERY_WEATHER, DemoSlice.class.getName()); . }Copy the code
3. Process the request in Ability and call the setResult() method to temporarily store the return result.
@Override
protected void onActive(a) {... Intent resultIntent =new Intent();
setResult(0, resultIntent); //0 is the resultCode returned after the current Ability is destroyed.. }Copy the code