HarmonyOS实战开发:@ohos.app.ability.wantAgent (WantAgent模块)

app.ability.WantAgent模块提供了创建WantAgent实例、获取实例的用户ID、获取want信息、比较WantAgent实例和获取bundle名称等能力。该模块将会取代@ohos.wantAgent模块,建议优先使用本模块。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import WantAgent from '@ohos.app.ability.wantAgent';

WantAgent.getWantAgent

getWantAgent(info: WantAgentInfo, callback: AsyncCallback<WantAgent>): void

创建WantAgent(callback形式)。 创建失败返回的WantAgent为空值。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
infoWantAgentInfoWantAgent信息。
callbackAsyncCallback<WantAgent>创建WantAgent的回调方法。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed, error: ${JSON.stringify(err)}`);
    }
}
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为空值。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
infoWantAgentInfoWantAgent信息。

返回值:

类型说明
Promise<WantAgent>以Promise形式返回WantAgent。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

try {
    WantAgent.getWantAgent(wantAgentInfo).then((data) => {
    wantAgent = data;
}).catch((err: BusinessError) => {
    console.error('getWantAgent failed! ${err.code} ${err.message}');
});
} catch (err) {
    console.error('getWantAgent failed! ${err.code} ${err.message}');
}

WantAgent.getBundleName

getBundleName(agent: WantAgent, callback: AsyncCallback<string>): void

获取WantAgent实例的包名(callback形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
callbackAsyncCallback<string>获取WantAgent实例的包名的回调方法。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed ${JSON.stringify(wantAgent)}`);
    }
    //getBundleName回调
    let getBundleNameCallback = (err: BusinessError, data: string) => {
        if(err) {
            console.error(`getBundleName failed! ${err.code} ${err.message}`);
        } else {
            console.info(`getBundleName ok! ${JSON.stringify(data)}`);
        }
    }
    try {
        WantAgent.getBundleName(wantAgent, getBundleNameCallback);
    } catch(err) {
        console.error(`getBundleName failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.getBundleName

getBundleName(agent: WantAgent): Promise<string>

获取WantAgent实例的包名(Promise形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。

返回值:

类型说明
Promise<string>以Promise形式返回获取WantAgent实例的包名。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

 //wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed! ${JSON.stringify(wantAgent)}`);
    }
    try {
        WantAgent.getBundleName(wantAgent).then((data)=>{
            console.info(`getBundleName ok! ${JSON.stringify(data)}`);
        }).catch((err: BusinessError)=>{
            console.error(`getBundleName failed! ${err.code} ${err.message}`);
        });
    } catch(err){
        console.error(`getBundleName failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.getUid

getUid(agent: WantAgent, callback: AsyncCallback<number>): void

获取WantAgent实例的用户ID(callback形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
callbackAsyncCallback<number>获取WantAgent实例的用户ID的回调方法。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.info(`getWantAgent failed ${JSON.stringify(err)}`);
    }
    //getUid回调
    let getUidCallback = (err: BusinessError, data: number) => {
        if(err) {
            console.error(`getUid failed! ${err.code} ${err.message}`);
        } else {
            console.info(`getUid ok! ${JSON.stringify(data)}`);
        }
    }
    try {
        WantAgent.getUid(wantAgent, getUidCallback);
    } catch(err) {
        console.error(`getUid failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.getUid

getUid(agent: WantAgent): Promise<number>

获取WantAgent实例的用户ID(Promise形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。

返回值:

类型说明
Promise<number>以Promise形式返回获取WantAgent实例的用户ID。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed! ${JSON.stringify(wantAgent)}`);
    }
    try {
        WantAgent.getUid(wantAgent).then((data)=>{
            console.info(`getUid ok! ${JSON.stringify(data)}`);
        }).catch((err: BusinessError)=>{
            console.error(`getUid failed! ${err.code} ${err.message}`);
        });
    } catch(err){
        console.error(`getUid failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.getWant

getWant(agent: WantAgent, callback: AsyncCallback<Want>): void

获取WantAgent对象的want(callback形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
callbackAsyncCallback<Want>获取WantAgent对象want的回调方法。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000015Service timeout.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed ${JSON.stringify(wantAgent)}`);
    }
    //getWant回调
    let getWantCallback = (err: BusinessError, data: Want) => {
        if(err) {
            console.error(`getWant failed! ${err.code} ${err.message}`);
        } else {
            console.info(`getWant ok! ${JSON.stringify(data)}`);
        }
    }
    try {
        WantAgent.getWant(wantAgent, getWantCallback);
    } catch(err) {
        console.error(`getWant failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.getWant

getWant(agent: WantAgent): Promise<Want>

获取WantAgent对象的want(Promise形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。

返回值:

类型说明
Promise<Want>以Promise形式返回获取WantAgent对象的want。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000015Service timeout.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed! ${JSON.stringify(wantAgent)}`);
    }
    try {
        WantAgent.getUid(wantAgent).then((data)=>{
            console.info(`getUid ok! ${JSON.stringify(data)}`);
        }).catch((err: BusinessError)=>{
            console.error(`getUid failed! ${err.code} ${err.message}`);
        });
    } catch(err){
        console.error(`getUid failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}}`);
}

WantAgent.cancel

cancel(agent: WantAgent, callback: AsyncCallback<void>): void

取消WantAgent实例(callback形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
callbackAsyncCallback<void>取消WantAgent实例的回调方法。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed ${JSON.stringify(wantAgent)}`);
    }
    //cancel回调
    let cancelCallback = (err: BusinessError, data: void) => {
        if(err) {
            console.error(`cancel failed! ${err.code} ${err.message}`);
        } else {
            console.info(`cancel ok!`);
        }
    }
    try {
        WantAgent.cancel(wantAgent, cancelCallback);
    } catch(err) {
        console.error(`cancel failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.cancel

cancel(agent: WantAgent): Promise<void>

取消WantAgent实例(Promise形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。

返回值:

类型说明
Promise<void>以Promise形式获取异步返回结果。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed! ${JSON.stringify(wantAgent)}`);
    }
    try {
        WantAgent.cancel(wantAgent).then((data)=>{
            console.info('cancel ok!');
        }).catch((err: BusinessError)=>{
            console.error(`cancel failed! ${err.code} ${err.message}`);
        });
    } catch(err){
        console.error(`cancel failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.trigger

trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: AsyncCallback<CompleteData>): void

主动激发WantAgent实例(callback形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
triggerInfoTriggerInfoTriggerInfo对象。
callbackAsyncCallback<CompleteData>主动激发WantAgent实例的回调方法。

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _WantAgent;
// triggerInfo
let triggerInfo: WantAgent.TriggerInfo = {
    code: 0 //自定义义结果码
};
//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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed ${JSON.stringify(wantAgent)}`);
    }
    //trigger回调
    let triggerCallback = (err: BusinessError, data: WantAgent.CompleteData) => {
        if(err) {
            console.error(`getUid failed! ${err.code} ${err.message}`);
        } else {
            console.info(`getUid ok! ${JSON.stringify(data)}`);
        }
    }
    try {
        WantAgent.trigger(wantAgent, triggerInfo, triggerCallback);
    } catch(err) {
        console.error(`getUid failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.equal

equal(agent: WantAgent, otherAgent: WantAgent, callback: AsyncCallback<boolean>): void

判断两个WantAgent实例是否相等(Callback形式),以此来判断是否是来自同一应用的相同操作。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
otherAgentWantAgentWantAgent对象。
callbackAsyncCallback<boolean>判断两个WantAgent实例是否相等的回调方法。

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent1: _WantAgent;
let wantAgent2: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent1 = data;
        wantAgent2 = data;
    } else {
        console.error(`getWantAgent failed ${JSON.stringify(wantAgent)}`);
    }
    //equal回调
    let equalCallback = (err: BusinessError, data: boolean) => {
        if(err) {
            console.error(`equal failed! ${err.code} ${err.message}`);
        } else {
            console.info(`equal ok! ${JSON.stringify(data)}`);
        }
    }
    try {
        WantAgent.equal(wantAgent1,wantAgent2,equalCallback);
    } catch(err) {
        console.error(`equal failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.equal

equal(agent: WantAgent, otherAgent: WantAgent): Promise<boolean>

判断两个WantAgent实例是否相等(Promise形式),以此来判断是否是来自同一应用的相同操作。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
otherAgentWantAgentWantAgent对象。

返回值:

类型说明
Promise<boolean>以Promise形式返回获取判断两个WantAgent实例是否相等的结果。

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent1: _WantAgent;
let wantAgent2: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent1 = data;
        wantAgent2 = data;
    } else {
        console.error(`getWantAgent failed! ${JSON.stringify(wantAgent)}`);
    }
    try {
        WantAgent.equal(wantAgent1,wantAgent2).then((data)=>{
            console.info(`equal ok! ${JSON.stringify(data)}`);
        }).catch((err: BusinessError)=>{
            console.error(`equal failed! ${err.code} ${err.message}`);
        })
    } catch(err){
        console.error(`equal failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.getOperationType

getOperationType(agent: WantAgent, callback: AsyncCallback<number>): void;

获取一个WantAgent的OperationType信息(callback形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。
callbackAsyncCallback<number>获取一个WantAgent的OperationType信息的回调方法。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000015Service timeout.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed ${JSON.stringify(wantAgent)}`);
    }
    //getOperationTypeCallback回调
    let getOperationTypeCallback = (err: BusinessError, data: number) => {
        if(err) {
            console.error(`getOperationType failed! ${err.code} ${err.message}`);
        } else {
            console.info(`getOperationType ok! ${JSON.stringify(data)}`);
        }
    }
    try {
        WantAgent.getOperationType(wantAgent, getOperationTypeCallback);
    } catch(err) {
        console.error(`getOperationTypeCallback failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgent.getOperationType

getOperationType(agent: WantAgent): Promise<number>;

获取一个WantAgent的OperationType信息(Promise形式)。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
agentWantAgentWantAgent对象。

返回值:

类型说明
Promise<number>以Promise形式返回获取operationType的结果。

错误码:

错误码ID错误信息
16000007Service busy, there are concurrent tasks, waiting for retry.
16000015Service timeout.
16000151Invalid wantagent object.

错误码详细介绍请参考元能力子系统错误码

示例:

import WantAgent, { WantAgent as _WantAgent} from '@ohos.app.ability.wantAgent';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

//wantAgent对象
let wantAgent: _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
    ],
    operationType: WantAgent.OperationType.START_ABILITIES,
    requestCode: 0,
    wantAgentFlags:[WantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

//getWantAgent回调
function getWantAgentCallback(err: BusinessError, data: _WantAgent) {
    if (err === undefined) {
        wantAgent = data;
    } else {
        console.error(`getWantAgent failed! ${JSON.stringify(wantAgent)}`);
    }
    try {
        WantAgent.getOperationType(wantAgent).then((data)=>{
            console.info(`getOperationType ok! ${JSON.stringify(data)}`);
        }).catch((err: BusinessError) => {
            console.error(`getOperationType failed! ${err.code} ${err.message}`);
        });
    } catch(err){
        console.error(`getOperationType failed! ${err.code} ${err.message}`);
    }
}
try {
    WantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback);
} catch(err) {
    console.error(`getWantAgent failed! ${err.code} ${err.message}`);
}

WantAgentFlags

系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core

名称说明
ONE_TIME_FLAG0WantAgent仅能使用一次。
NO_BUILD_FLAG1如果描述WantAgent对象不存在,则不创建它,直接返回null。
CANCEL_PRESENT_FLAG2在生成一个新的WantAgent对象前取消已存在的一个WantAgent对象。
UPDATE_PRESENT_FLAG3使用新的WantAgent的额外数据替换已存在的WantAgent中的额外数据。
CONSTANT_FLAG4WantAgent是不可变的。
REPLACE_ELEMENT5当前Want中的element属性可被WantAgent.trigger()中Want的element属性取代,当前版本暂不支持。
REPLACE_ACTION6当前Want中的action属性可被WantAgent.trigger()中Want的action属性取代,当前版本暂不支持。
REPLACE_URI7当前Want中的uri属性可被WantAgent.trigger()中Want的uri属性取代,当前版本暂不支持。
REPLACE_ENTITIES8当前Want中的entities属性可被WantAgent.trigger()中Want的entities属性取代,当前版本暂不支持。
REPLACE_BUNDLE9当前Want中的bundleName属性可被WantAgent.trigger()中Want的bundleName属性取代,当前版本暂不支持。

OperationType

系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core

名称说明
UNKNOWN_TYPE0不识别的类型。
START_ABILITY1开启一个有页面的Ability。
START_ABILITIES2开启多个有页面的Ability。
START_SERVICE3开启一个无页面的ability。
SEND_COMMON_EVENT4发送一个公共事件。

CompleteData

系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core

名称类型必填说明
infoWantAgent触发的wantAgent。
wantWant存在的被触发的want。
finalCodenumber触发wantAgent的请求代码。
finalDatastring公共事件收集的最终数据。
extraInfo{[key: string]: any}额外数据。

最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

鸿蒙(HarmonyOS NEXT)最新学习路线

  •  HarmonOS基础技能

  • HarmonOS就业必备技能 
  •  HarmonOS多媒体技术

  • 鸿蒙NaPi组件进阶

  • HarmonOS高级技能

  • 初识HarmonOS内核 
  • 实战就业级设备开发

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

《鸿蒙 (OpenHarmony)开发入门教学视频》

《鸿蒙生态应用开发V2.0白皮书》

图片

《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

图片

 《鸿蒙开发基础》

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……

图片

 《鸿蒙开发进阶》

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

图片

《鸿蒙进阶实战》

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

图片

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料

总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值