ability-startup-with-implicit-want.md 2.6 KB
Newer Older
Z
zengyawen 已提交
1 2
# 使用隐式Want打开网址

3
以打开浏览器为例,假设设备上安装了一个或多个浏览器应用。为了使浏览器应用能够正常工作,需要在[module.json5配置文件](../quick-start/module-configuration-file.md)进行配置,具体配置如下:
Z
zengyawen 已提交
4 5

```json
6 7 8 9
{
  "module": {
    // ...
    "abilities": [
Z
zengyawen 已提交
10 11
      {
        // ...
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        "skills": [
          {
            "entities": [
              "entity.system.home",
              "entity.system.browsable"
              // ...
            ],
            "actions": [
              "action.system.home",
              "ohos.want.action.viewData"
              // ...
            ],
            "uris": [
              {
                "scheme": "https",
                "host": "www.test.com",
                "port": "8080",
                // prefix matching
                "pathStartWith": "query",
                "type": "text/*"
              },
              {
                "scheme": "http",
                // ...
              }
              // ...
            ]
          }
        ]
Z
zengyawen 已提交
41 42
      }
    ]
43 44
  }
}
Z
zengyawen 已提交
45 46
```

47
在调用方UIAbility中,使用隐式Want方式启动浏览器应用。
Z
zengyawen 已提交
48

49 50
```ts
import common from '@ohos.app.ability.common';
Z
zengyawen 已提交
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
function implicitStartAbility() {
  let context = getContext(this) as common.UIAbilityContext;
  let wantInfo = {
    // uncomment line below if wish to implicitly query only in the specific bundle.
    // bundleName: "com.example.myapplication",
    "action": "ohos.want.action.viewData",
    // entities can be omitted.
    "entities": ["entity.system.browsable"],
    "uri": "https://www.test.com:8080/query/student",
    "type": "text/plain"
  }
  context.startAbility(wantInfo).then(() => {
    // ...
  }).catch((err) => {
    // ...
  })
}
```
70

71
匹配过程分析:
Z
zengyawen 已提交
72

73 74 75 76
1. 调用方传入的want参数的action不为空,待匹配Ability的skills配置中的actions不为空且包含调用方传入的want参数的action,action匹配成功。
2. 调用方传入的want参数的entities不为空,待匹配Ability的skills配置中的entities不为空且包含调用方传入的want参数的entities,entities匹配成功。
3. 待匹配Ability的skills配置中内uris拼接为`https://www.test.com:8080/query*` (其中*表示通配符),包含调用方传入的want参数的uri,uri匹配成功。
4. 调用方传入的want参数的type不为空,待匹配Ability的skills配置中的type不为空且包含调用方传入的want参数的type,type匹配成功。
77

78
当有多个匹配应用时,会被应用选择器展示给用户进行选择。 示意效果如下图所示。  
79
![](figures/ability-startup-with-implicit-want2.png)