• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import wantAgent from '@ohos.app.ability.wantAgent'
17
18const REQUEST_CODE: number = 0 // WantAgentInfo的请求码,默认定义成0
19class WantAgentUtil {
20  /**
21   * create wantAgent for start ability
22   *
23   * @param bundleName
24   * @param abilityName
25   * @return return the created WantAgent object.
26   */
27  async createWantAgentForStartAbility(bundleName: string, abilityName: string) {
28    let wantAgentInfo: wantAgent.WantAgentInfo = {
29      wants: [
30        {
31          bundleName: bundleName,
32          abilityName: abilityName
33        }
34      ],
35      operationType: wantAgent.OperationType.START_ABILITY,
36      requestCode: REQUEST_CODE // requestCode是WantAgentInfo的请求码,是使用者定义的一个私有值
37    }
38    return await wantAgent.getWantAgent(wantAgentInfo);
39  }
40
41  /**
42   * create wantAgent for common event
43   *
44   * @param mAction
45   * @return return the created WantAgent object.
46   */
47  async createWantAgentForCommonEvent(action: string) {
48    let wantAgentInfo: wantAgent.WantAgentInfo = {
49      wants: [{ action: action }],
50      operationType: wantAgent.OperationType.SEND_COMMON_EVENT,
51      requestCode: REQUEST_CODE // requestCode是WantAgentInfo的请求码,是使用者定义的一个私有值
52    }
53    return await wantAgent.getWantAgent(wantAgentInfo);
54  }
55}
56
57export let wantAgentUtil = new WantAgentUtil();