js-apis-app-ability-abilityManager.md 13.4 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.app.ability.abilityManager (AbilityManager)
D
donglin 已提交
2

D
donglin 已提交
3
AbilityManager模块提供获取、新增、修改Ability相关信息和状态信息进行的能力。
D
donglin 已提交
4 5 6 7 8 9 10 11

> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。  
> 本模块接口均为系统接口,三方应用不支持调用。

## 导入模块

M
m00512953 已提交
12
```ts
13
import abilityManager from '@ohos.app.ability.abilityManager';
D
donglin 已提交
14 15 16 17
```

## AbilityState

D
donglin 已提交
18
Ability的状态,该类型为枚举,可配合[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md)返回Abiltiy的状态。
D
donglin 已提交
19 20 21

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

22
**系统API**: 此枚举类型为系统接口内部定义,三方应用不支持调用。
D
donglin 已提交
23 24 25

| 名称 | 值 | 说明 | 
| -------- | -------- | -------- |
D
donglin 已提交
26
| INITIAL | 0 | 表示ability为初始化状态。| 
D
dy_study 已提交
27
| FOCUS | 2 | 表示ability为获焦状态。 |
D
donglin 已提交
28 29 30 31
| FOREGROUND | 9 | 表示ability为前台状态。  | 
| BACKGROUND | 10 | 表示ability为后台状态。  | 
| FOREGROUNDING | 11 | 表示ability为前台调度中状态。  | 
| BACKGROUNDING | 12 | 表示ability为后台调度中状态。  | 
D
donglin 已提交
32 33 34 35 36

## updateConfiguration

updateConfiguration(config: Configuration, callback: AsyncCallback\<void>): void

D
donglin 已提交
37
通过传入修改的配置项来更新配置(callback形式)。
D
donglin 已提交
38 39 40 41 42 43 44

**需要权限**: ohos.permission.UPDATE_CONFIGURATION

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

D
fix  
donglin 已提交
45
| 参数名        | 类型                                       | 必填   | 说明             |
D
donglin 已提交
46
| --------- | ---------------------------------------- | ---- | -------------- |
D
donglin 已提交
47
| config    | [Configuration](js-apis-app-ability-configuration.md)   | 是    | 新的配置项,仅需配置需要更新的项。 |
D
donglin 已提交
48
| callback  | AsyncCallback\<void>                   | 是    | 以回调方式返回接口运行结果,可进行错误处理或其他自定义处理。      |
D
donglin 已提交
49

D
donglin 已提交
50 51 52 53 54 55 56 57
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
58 59
**示例**

M
m00512953 已提交
60
```ts
61 62
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
63 64 65 66
const config = {
  language: 'Zh-Hans',                 // 简体中文
  colorMode: COLOR_MODE_LIGHT,         // 浅色模式
  direction: DIRECTION_VERTICAL,       // 垂直方向
M
mingxihua 已提交
67
  screenDensity: SCREEN_DENSITY_SDPI,  // 屏幕像素密度为'sdpi'
D
donglin 已提交
68 69
  displayId: 1,                        // 应用在Id为1的物理屏上显示
  hasPointerDevice: true,              // 指针类型设备已连接
70
};
D
donglin 已提交
71 72

try {
D
donglin 已提交
73
    abilityManager.updateConfiguration(config, (err) => {
D
donglin 已提交
74
        if (err && err.code !== 0) {
M
mingxihua 已提交
75
            console.log('updateConfiguration fail, err: ${JSON.stringify(err)}');
D
donglin 已提交
76
        } else {
M
mingxihua 已提交
77
            console.log('updateConfiguration success.');
D
donglin 已提交
78
        }
M
mingxihua 已提交
79
    });
D
donglin 已提交
80
} catch (paramError) {
M
mingxihua 已提交
81
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
D
donglin 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}
```

## updateConfiguration

updateConfiguration(config: Configuration): Promise\<void>

通过修改配置来更新配置(Promise形式)。

**需要权限**: ohos.permission.UPDATE_CONFIGURATION

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

**参数**

D
fix  
donglin 已提交
97
| 参数名        | 类型                                       | 必填   | 说明             |
D
donglin 已提交
98
| --------- | ---------------------------------------- | ---- | -------------- |
D
donglin 已提交
99
| config    | [Configuration](js-apis-app-ability-configuration.md)   | 是    | 新的配置项,仅需配置需要更新的项。 |
D
donglin 已提交
100 101 102 103 104

**返回值:**

| 类型                                       | 说明      |
| ---------------------------------------- | ------- |
D
donglin 已提交
105
| Promise\<void> | 以Promise方式返回接口运行结果息,可进行错误处理或其他自定义处理。 |
D
donglin 已提交
106

D
donglin 已提交
107 108 109 110 111 112 113 114
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
115 116
**示例**

M
m00512953 已提交
117
```ts
118 119
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
120 121 122 123
const config = {
  language: 'Zh-Hans',                 // 简体中文
  colorMode: COLOR_MODE_LIGHT,         // 浅色模式
  direction: DIRECTION_VERTICAL,       // 垂直方向
M
mingxihua 已提交
124
  screenDensity: SCREEN_DENSITY_SDPI,  // 屏幕像素密度为'sdpi'
D
donglin 已提交
125 126
  displayId: 1,                        // 应用在Id为1的物理屏上显示
  hasPointerDevice: true,              // 指针类型设备已连接
127
};
D
donglin 已提交
128 129

try {
D
donglin 已提交
130 131 132
    abilityManager.updateConfiguration(config).then(() => {
        console.log('updateConfiguration success.');
    }).catch((err) => {
M
mingxihua 已提交
133
        console.log('updateConfiguration fail, err: ${JSON.stringify(err)}');
M
mingxihua 已提交
134
    });
D
donglin 已提交
135
} catch (paramError) {
M
mingxihua 已提交
136
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
D
donglin 已提交
137 138 139 140 141 142 143
}
```

## getAbilityRunningInfos

getAbilityRunningInfos(callback: AsyncCallback\<Array\<AbilityRunningInfo>>): void

D
dy_study 已提交
144
获取UIAbility运行相关信息(callback形式)。
D
donglin 已提交
145 146 147 148 149 150 151

**需要权限**: ohos.permission.GET_RUNNING_INFO

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

**参数**

D
fix  
donglin 已提交
152
| 参数名        | 类型                                       | 必填   | 说明             |
D
donglin 已提交
153
| --------- | ---------------------------------------- | ---- | -------------- |
D
donglin 已提交
154
| callback  | AsyncCallback\<Array\<[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md)>>  | 是    | 以回调方式返回接口运行结果及运行中的ability信息,可进行错误处理或其他自定义处理。      |
D
donglin 已提交
155

156 157 158 159 160 161 162 163
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
164 165
**示例**

M
m00512953 已提交
166
```ts
167 168
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
169
try {
D
donglin 已提交
170
    abilityManager.getAbilityRunningInfos((err, data) => {
D
donglin 已提交
171
        if (err && err.code !== 0) {
M
mingxihua 已提交
172
            console.log('getAbilityRunningInfos fail, error: ${JSON.stringify(err)}');
D
donglin 已提交
173
        } else {
M
mingxihua 已提交
174
            console.log('getAbilityRunningInfos success, data: ${JSON.stringify(data)}');
D
donglin 已提交
175 176
        }
    });
D
donglin 已提交
177
} catch (paramError) {
M
mingxihua 已提交
178
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
D
donglin 已提交
179 180 181 182 183 184 185
}
```

## getAbilityRunningInfos

getAbilityRunningInfos(): Promise\<Array\<AbilityRunningInfo>>

D
dy_study 已提交
186
获取UIAbility运行相关信息(Promise形式)。
D
donglin 已提交
187 188 189 190 191 192 193 194 195

**需要权限**: ohos.permission.GET_RUNNING_INFO

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

**返回值:**

| 类型                                       | 说明      |
| ---------------------------------------- | ------- |
D
donglin 已提交
196
| Promise\<Array\<[AbilityRunningInfo](js-apis-inner-application-abilityRunningInfo.md)>> | 以Promise方式返回接口运行结果及运行中的ability信息,可进行错误处理或其他自定义处理。 |
D
donglin 已提交
197

D
donglin 已提交
198 199 200 201 202 203 204 205
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
206 207
**示例**

M
m00512953 已提交
208
```ts
209 210
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
211
try {
D
donglin 已提交
212
    abilityManager.getAbilityRunningInfos().then((data) => {
M
mingxihua 已提交
213
        console.log('getAbilityRunningInfos success, data: ${JSON.stringify(data)}');
D
donglin 已提交
214
    }).catch((err) => {
M
mingxihua 已提交
215
        console.log('getAbilityRunningInfos fail, err: ${JSON.stringify(err)}');
D
donglin 已提交
216
    });
D
donglin 已提交
217
} catch (paramError) {
M
mingxihua 已提交
218
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
D
donglin 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
}
```

## getExtensionRunningInfos

getExtensionRunningInfos(upperLimit: number, callback: AsyncCallback\<Array\<ExtensionRunningInfo>>): void

获取关于运行扩展能力的信息(callback形式)。

**需要权限**: ohos.permission.GET_RUNNING_INFO

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

**参数**

D
fix  
donglin 已提交
234
| 参数名        | 类型                                       | 必填   | 说明             |
D
donglin 已提交
235
| --------- | ---------------------------------------- | ---- | -------------- |
D
donglin 已提交
236
| upperLimit | number                                   | 是 | 获取消息数量的最大限制,最大为2<sup>31</sup>-1。 |
D
donglin 已提交
237
| callback  | AsyncCallback\<Array\<[ExtensionRunningInfo](js-apis-inner-application-extensionRunningInfo.md)>>  | 是    | 以回调方式返回接口运行结果及运行中的extension信息,可进行错误处理或其他自定义处理。      |
D
donglin 已提交
238

D
donglin 已提交
239 240 241 242 243 244 245 246
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
247 248
**示例**

M
m00512953 已提交
249
```ts
250 251
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
252
let upperLimit = 10;
D
donglin 已提交
253 254

try {
D
donglin 已提交
255
    abilityManager.getExtensionRunningInfos(upperLimit, (err, data) => { 
D
donglin 已提交
256
        if (err && err.code !== 0) {
M
mingxihua 已提交
257
            console.log('getExtensionRunningInfos fail, err: ${JSON.stringify(err)}');
D
donglin 已提交
258
        } else {
M
mingxihua 已提交
259
            console.log('getExtensionRunningInfos success, data: ${JSON.stringify(data)}');
D
donglin 已提交
260 261
        }
    });
D
donglin 已提交
262
} catch (paramError) {
M
mingxihua 已提交
263
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
D
donglin 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
}
```

## getExtensionRunningInfos

getExtensionRunningInfos(upperLimit: number): Promise\<Array\<ExtensionRunningInfo>>

获取关于运行扩展能力的信息(Promise形式)。
 
**需要权限**: ohos.permission.GET_RUNNING_INFO

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

**参数**

D
fix  
donglin 已提交
279
| 参数名        | 类型                                       | 必填   | 说明             |
D
donglin 已提交
280
| --------- | ---------------------------------------- | ---- | -------------- |
D
donglin 已提交
281
| upperLimit | number                                   | 是 | 获取消息数量的最大限制,最大为2<sup>31</sup>-1。 |
D
donglin 已提交
282 283 284 285 286

**返回值:**

| 类型                                       | 说明      |
| ---------------------------------------- | ------- |
D
donglin 已提交
287
| Promise\<Array\<[ExtensionRunningInfo](js-apis-inner-application-extensionRunningInfo.md)>> | 以Promise方式返回接口运行结果及运行中的extension信息,可进行错误处理或其他自定义处理。 |
D
donglin 已提交
288

D
donglin 已提交
289 290 291 292 293 294 295 296
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
297 298
**示例**

M
m00512953 已提交
299
```ts
300 301
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
302
let upperLimit = 10;
D
donglin 已提交
303 304

try {
D
donglin 已提交
305
    abilityManager.getExtensionRunningInfos(upperLimit).then((data) => {
M
mingxihua 已提交
306
        console.log('getExtensionRunningInfos success, data: ${JSON.stringify(data)}');
D
donglin 已提交
307
    }).catch((err) => {
M
mingxihua 已提交
308
        console.log('getExtensionRunningInfos fail, err: ${JSON.stringify(err)}');
M
mingxihua 已提交
309
    });
D
donglin 已提交
310
} catch (paramError) {
M
mingxihua 已提交
311
    console.log('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
D
donglin 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324
}
```

## getTopAbility<sup>9+</sup>

getTopAbility(callback: AsyncCallback\<ElementName>): void;

获取窗口焦点的ability接口(callback形式)。

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

**参数**

D
fix  
donglin 已提交
325
| 参数名        | 类型                                       | 必填   | 说明             |
D
donglin 已提交
326
| --------- | ---------------------------------------- | ---- | -------------- |
D
donglin 已提交
327
| callback  | AsyncCallback\<[ElementName](js-apis-bundleManager-elementName.md)>  | 是    | 以回调方式返回接口运行结果及应用名,可进行错误处理或其他自定义处理。      |
D
donglin 已提交
328

D
donglin 已提交
329 330 331 332 333 334 335 336
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
337 338
**示例**

M
m00512953 已提交
339
```ts
340 341
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
342
abilityManager.getTopAbility((err, data) => { 
D
donglin 已提交
343
    if (err && err.code !== 0) {
M
mingxihua 已提交
344
        console.log('getTopAbility fail, err: ${JSON.stringify(err)}');
D
donglin 已提交
345
    } else {
M
mingxihua 已提交
346
        console.log('getTopAbility success, data: ${JSON.stringify(data)}');
D
donglin 已提交
347
    }
D
donglin 已提交
348
});
D
donglin 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362
```

## getTopAbility

getTopAbility(): Promise\<ElementName>;

获取窗口焦点的ability接口(Promise形式)。
 
**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core

**返回值:**

| 类型                                       | 说明      |
| ---------------------------------------- | ------- |
D
donglin 已提交
363
| Promise\<[ElementName](js-apis-bundleManager-elementName.md)>| 以Promise方式返回接口运行结果及应用名,可进行错误处理或其他自定义处理。 |
D
donglin 已提交
364

D
donglin 已提交
365 366 367 368 369 370 371 372
**错误码**

| 错误码ID | 错误信息 |
| ------- | -------- |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

D
donglin 已提交
373 374
**示例**

M
m00512953 已提交
375
```ts
376 377
import abilityManager from '@ohos.app.ability.abilityManager';

D
donglin 已提交
378
abilityManager.getTopAbility().then((data) => {
M
mingxihua 已提交
379
    console.log('getTopAbility success, data: ${JSON.stringify(data)}');
D
donglin 已提交
380
}).catch((err) => {
M
mingxihua 已提交
381
    console.log('getTopAbility fail, err: ${JSON.stringify(err)}');
M
mingxihua 已提交
382
});
D
donglin 已提交
383
```