往期鸿蒙全套实战文章必看:(文中附带全栈鸿蒙学习资料)
@ohos.app.ability.wantAgent (WantAgent模块)
app.ability.WantAgent是一个封装了Want对象的类,允许应用程序能够在未来的某个时间点执行该Want。该模块提供了创建WantAgent实例、获取实例的用户ID、获取want信息、比较WantAgent实例和获取包名等功能。
WantAgent的一个典型应用场景是通知处理。例如,当用户点击通知时,会触发WantAgent的trigger接口,并拉起目标应用。该模块将会取代@ohos.wantAgent模块,建议优先使用本模块。
说明
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
import { wantAgent } from '@kit.AbilityKit';
wantAgent.getWantAgent
getWantAgent(info: WantAgentInfo, callback: AsyncCallback<WantAgent>): void
创建WantAgent(callback形式)。 创建失败返回的WantAgent为空值。
三方应用只能设置自己应用的Ability。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
info | WantAgentInfo | 是 | WantAgent信息。 |
callback | AsyncCallback<WantAgent> | 是 | 创建WantAgent的回调方法。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
16000007 | Service busy, there are concurrent tasks, waiting for retry. |
16000151 | Invalid wantagent object. |
示例:
import { wantAgent, Want } from '@kit.AbilityKit';
import type { WantAgent } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
//wantAgent对象
let wantAgentData: WantAgent;
//WantAgentInfo对象
let wantAgentInfo: wantAgent.WantAgentInfo = {
wants: [
{
deviceId: 'deviceId',
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
action: 'action1',
entities: ['entity1'],
type: 'MIMETYPE',
uri: 'key={true,true,false}',
parameters:
{
mykey0: 2222,
mykey1: [1, 2, 3],
mykey2: '[1, 2, 3]',
mykey3: 'ssssssssssssssssssssssssss',
mykey4: [false, true, false],
mykey5: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
mykey6: true,
}
} as Want
],
actionType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: WantAgent) {
if (err) {
console.error(`getWantAgent failed, code: ${JSON.stringify(err.code)}, message: ${JSON.stringify(err.message)}`);
} else {
wantAgentData = data;
}
}
try {
wantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch (err) {
console.error(`getWantAgent failed, error: ${JSON.stringify(err)}`);
}
wantAgent.getWantAgent
getWantAgent(info: WantAgentInfo): Promise<WantAgent>
创建WantAgent(Promise形式)。 创建失败返回的WantAgent为空值。
三方应用只能设置自己应用的Ability。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
info | WantAgentInfo | 是 | WantAgent信息。 |
返回值:
类型 | 说明 |
---|---|
Promise<WantAgent> | 以Promise形式返回WantAgent。 |
错误码:
以下错误码详细介绍。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
16000007 | Service busy, there are concurrent tasks, waiting for retry. |
16000151 | Invalid want |