1# Functions 2<!--Kit: ArkUI--> 3<!--Subsystem: Window--> 4<!--Owner: @waterwin--> 5<!--Designer: @nyankomiya--> 6<!--Tester: @qinliwen0417--> 7<!--Adviser: @ge-yafang--> 8 9> **说明:** 10> 11> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12 13## 导入模块 14 15```ts 16import { window } from '@kit.ArkUI'; 17``` 18 19## window.createWindow<sup>9+</sup> 20 21createWindow(config: Configuration, callback: AsyncCallback<Window>): void 22 23创建子窗口或者系统窗口,使用callback异步回调。 24 25非[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下,子窗口创建后默认是[沉浸式布局](../../windowmanager/window-terminology.md#沉浸式布局)。 26 27自由窗口状态下,子窗口参数[decorEnabled](arkts-apis-window-i.md#configuration9)为false时,子窗口创建后为沉浸式布局;子窗口参数decorEnabled为true,子窗口创建后为非沉浸式布局。 28 29**需要权限:** ohos.permission.SYSTEM_FLOAT_WINDOW(仅当创建窗口类型为window.WindowType.TYPE_FLOAT时需要申请) 30 31**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 32 33**系统能力:** SystemCapability.WindowManager.WindowManager.Core 34 35**参数:** 36 37| 参数名 | 类型 | 必填 | 说明 | 38| -------- | -------------------------------------- | -- | --------------------------------- | 39| config | [Configuration](arkts-apis-window-i.md#configuration9) | 是 | 创建窗口时的参数。 | 40| callback | AsyncCallback<[Window](arkts-apis-window-Window.md)> | 是 | 回调函数。返回当前创建的窗口对象。 | 41 42**错误码:** 43 44以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 45 46| 错误码ID | 错误信息 | 47| ------- | -------------------------------- | 48| 201 | Permission verification failed. The application does not have the permission required to call the API. | 49| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 50| 801 | Capability not supported. createWindow can not work correctly due to limited device capabilities. | 51| 1300001 | Repeated operation. | 52| 1300002 | This window state is abnormal. | 53| 1300004 | Unauthorized operation. | 54| 1300006 | This window context is abnormal. | 55| 1300009 | The parent window is invalid. | 56 57**示例:** 58 59```ts 60import { UIAbility } from '@kit.AbilityKit'; 61import { window } from '@kit.ArkUI'; 62import { BusinessError } from '@kit.BasicServicesKit'; 63 64export default class EntryAbility extends UIAbility { 65 onWindowStageCreate(windowStage: window.WindowStage): void { 66 let windowClass: window.Window | undefined = undefined; 67 let config: window.Configuration = { 68 name: "test", 69 windowType: window.WindowType.TYPE_DIALOG, 70 ctx: this.context 71 }; 72 try { 73 window.createWindow(config, (err: BusinessError, data) => { 74 const errCode: number = err.code; 75 if (errCode) { 76 console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`); 77 return; 78 } 79 windowClass = data; 80 console.info('Succeeded in creating the window. Data: ' + JSON.stringify(data)); 81 windowClass.resize(500, 1000); 82 }); 83 } catch (exception) { 84 console.error(`Failed to create the window. Cause code: ${exception.code}, message: ${exception.message}`); 85 } 86 } 87} 88``` 89 90## window.createWindow<sup>9+</sup> 91 92createWindow(config: Configuration): Promise<Window> 93 94创建子窗口或者系统窗口,使用Promise异步回调。 95 96非[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下,子窗口创建后默认是[沉浸式布局](../../windowmanager/window-terminology.md#沉浸式布局)。 97 98自由窗口状态下,子窗口参数[decorEnabled](arkts-apis-window-i.md#configuration9)为false时,子窗口创建后为沉浸式布局;子窗口参数decorEnabled为true,子窗口创建后为非沉浸式布局。 99 100**需要权限:** ohos.permission.SYSTEM_FLOAT_WINDOW(仅当创建窗口类型为window.WindowType.TYPE_FLOAT时需要申请) 101 102**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 103 104**系统能力:** SystemCapability.WindowManager.WindowManager.Core 105 106**参数:** 107 108| 参数名 | 类型 | 必填 | 说明 | 109| ------ | -------------------------------- | -- | ------------------ | 110| config | [Configuration](arkts-apis-window-i.md#configuration9) | 是 | 创建窗口时的参数。 | 111 112**返回值:** 113 114| 类型 | 说明 | 115| -------------------------------- | ------------------------------------ | 116| Promise<[Window](arkts-apis-window-Window.md)> | Promise对象。返回当前创建的窗口对象。 | 117 118**错误码:** 119 120以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 121 122| 错误码ID | 错误信息 | 123| ------- | -------------------------------- | 124| 201 | Permission verification failed. The application does not have the permission required to call the API. | 125| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 126| 801 | Capability not supported. createWindow can not work correctly due to limited device capabilities. | 127| 1300001 | Repeated operation. | 128| 1300002 | This window state is abnormal. | 129| 1300004 | Unauthorized operation. | 130| 1300006 | This window context is abnormal. | 131| 1300009 | The parent window is invalid. | 132 133**示例:** 134 135```ts 136import { UIAbility } from '@kit.AbilityKit'; 137import { window } from '@kit.ArkUI'; 138import { BusinessError } from '@kit.BasicServicesKit'; 139 140export default class EntryAbility extends UIAbility { 141 onWindowStageCreate(windowStage: window.WindowStage): void { 142 let config: window.Configuration = { 143 name: "test", 144 windowType: window.WindowType.TYPE_DIALOG, 145 ctx: this.context 146 }; 147 try { 148 window.createWindow(config).then((value:window.Window) => { 149 console.info('Succeeded in creating the window. Data: ' + JSON.stringify(value)); 150 value.resize(500, 1000); 151 }).catch((err:BusinessError)=> { 152 console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`); 153 }); 154 } catch (exception) { 155 console.error(`Failed to create the window. Cause code: ${exception.code}, message: ${exception.message}`); 156 } 157 } 158} 159``` 160 161## window.findWindow<sup>9+</sup> 162 163findWindow(name: string): Window 164 165查找name所对应的窗口。 166 167**系统能力:** SystemCapability.WindowManager.WindowManager.Core 168 169**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 170 171**参数:** 172 173| 参数名 | 类型 | 必填 | 说明 | 174| ------ | ------ | ---- | -------- | 175| name | string | 是 | 窗口名字,即[Configuration](arkts-apis-window-i.md#configuration9)中的name。 | 176 177**返回值:** 178 179| 类型 | 说明 | 180| ----------------- | ------------------- | 181| [Window](arkts-apis-window-Window.md) | 当前查找的窗口对象。 | 182 183**错误码:** 184 185以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 186 187| 错误码ID | 错误信息 | 188| ------- | -------------------------------- | 189| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 190| 1300002 | This window state is abnormal. | 191 192**示例:** 193 194```ts 195let windowClass: window.Window | undefined = undefined; 196try { 197 windowClass = window.findWindow('test'); 198} catch (exception) { 199 console.error(`Failed to find the Window. Cause code: ${exception.code}, message: ${exception.message}`); 200} 201``` 202 203## window.getLastWindow<sup>9+</sup> 204 205getLastWindow(ctx: BaseContext, callback: AsyncCallback<Window>): void 206 207获取当前应用内最上层显示的子窗口,使用callback异步回调。 208 209若无应用子窗口或子窗口未调用[showWindow()](arkts-apis-window-Window.md#showwindow9)进行显示,则返回应用主窗口。 210 211**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 212 213**系统能力:** SystemCapability.WindowManager.WindowManager.Core 214 215**参数:** 216 217| 参数名 | 类型 | 必填 | 说明 | 218| -------- | -------------------------------------- | -- | ---------------------------------------- | 219| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前应用上下文信息。 | 220| callback | AsyncCallback<[Window](arkts-apis-window-Window.md)> | 是 | 回调函数。返回当前应用内最上层的窗口对象。 | 221 222**错误码:** 223 224以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 225 226| 错误码ID | 错误信息 | 227| ------- | -------------------------------- | 228| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 229| 1300002 | This window state is abnormal. Top window or main window is null or destroyed. | 230| 1300006 | This window context is abnormal. | 231 232**示例:** 233 234```ts 235import { UIAbility } from '@kit.AbilityKit'; 236import { window } from '@kit.ArkUI'; 237import { BusinessError } from '@kit.BasicServicesKit'; 238 239export default class EntryAbility extends UIAbility { 240 // ... 241 onWindowStageCreate(windowStage: window.WindowStage): void { 242 console.info('onWindowStageCreate'); 243 windowStage.createSubWindow('TestSubWindow').then((subWindow) => { 244 subWindow.showWindow().then(() => { 245 try { 246 window.getLastWindow(this.context, (err: BusinessError, topWindow) => { 247 const errCode: number = err.code; 248 if (errCode) { 249 console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`); 250 return; 251 } 252 console.info(`Succeeded in obtaining the top window. Window id: ${topWindow.getWindowProperties().id}`); 253 }); 254 } catch (exception) { 255 console.error(`Failed to obtain the top window. Cause code: ${exception.code}, message: ${exception.message}`); 256 } 257 }); 258 }); 259 } 260 //... 261} 262``` 263 264## window.getLastWindow<sup>9+</sup> 265 266getLastWindow(ctx: BaseContext): Promise<Window> 267 268获取当前应用内最上层显示的子窗口,使用Promise异步回调。 269 270若无应用子窗口或子窗口未调用[showWindow()](arkts-apis-window-Window.md#showwindow9)进行显示,则返回应用主窗口。 271 272**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 273 274**系统能力:** SystemCapability.WindowManager.WindowManager.Core 275 276**参数:** 277 278| 参数名 | 类型 | 必填 | 说明 | 279| ------ | ----------- | ---- | ------------------------------------------------------------ | 280| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前应用上下文信息。 | 281 282**返回值:** 283 284| 类型 | 说明 | 285| -------------------------------- | ------------------------------------------- | 286| Promise<[Window](arkts-apis-window-Window.md)> | Promise对象。返回当前应用内最上层的窗口对象。 | 287 288**错误码:** 289 290以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 291 292| 错误码ID | 错误信息 | 293| ------- | -------------------------------- | 294| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 295| 1300002 | This window state is abnormal. Top window or main window is null or destroyed. | 296| 1300006 | This window context is abnormal. | 297 298**示例:** 299 300```ts 301// EntryAbility.ets 302import { UIAbility } from '@kit.AbilityKit'; 303import { window } from '@kit.ArkUI'; 304import { BusinessError } from '@kit.BasicServicesKit'; 305 306export default class EntryAbility extends UIAbility { 307 // ... 308 onWindowStageCreate(windowStage: window.WindowStage): void { 309 console.info('onWindowStageCreate'); 310 windowStage.createSubWindow('TestSubWindow').then((subWindow) => { 311 subWindow.showWindow().then(() => { 312 try { 313 window.getLastWindow(this.context).then((topWindow) => { 314 console.info(`Succeeded in obtaining the top window. Window id: ${topWindow.getWindowProperties().id}`); 315 }).catch((err: BusinessError) => { 316 console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`); 317 }); 318 } catch (exception) { 319 console.error(`Failed to obtain the top window. Cause code: ${exception.code}, message: ${exception.message}`); 320 } 321 }); 322 }); 323 } 324 //... 325} 326``` 327 328## window.shiftAppWindowFocus<sup>11+</sup> 329shiftAppWindowFocus(sourceWindowId: number, targetWindowId: number): Promise<void> 330 331在同应用内将窗口焦点从源窗口转移到目标窗口,仅支持应用主窗和子窗的焦点转移。 332 333目标窗口需确保可获焦属性为true(见[setWindowFocusable()](arkts-apis-window-Window.md#setwindowfocusable9)),并确保调用[showWindow()](arkts-apis-window-Window.md#showwindow9)成功并执行完毕。 334 335**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 336 337**系统能力:** SystemCapability.Window.SessionManager 338 339**参数:** 340 341| 参数名 | 类型 | 必填 | 说明 | 342| -------------- | ------ | ----- | ----------------------- | 343| sourceWindowId | number | 是 | 源窗口id,必须是获焦状态。| 344| targetWindowId | number | 是 | 目标窗口id。 | 345 346**返回值:** 347 348| 类型 | 说明 | 349| ------------------- | ------------------------- | 350| Promise<void> | 无返回结果的Promise对象。 | 351 352**错误码:** 353 354以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 355 356| 错误码ID | 错误信息 | 357| ------- | --------------------------------------------- | 358| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 359| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 360| 1300002 | This window state is abnormal. | 361| 1300003 | This window manager service works abnormally. | 362| 1300004 | Unauthorized operation. | 363 364**示例:** 365 366```ts 367// EntryAbility.ets 368import { UIAbility } from '@kit.AbilityKit'; 369import { window } from '@kit.ArkUI'; 370import { BusinessError } from '@kit.BasicServicesKit'; 371 372export default class EntryAbility extends UIAbility { 373 onWindowStageCreate(windowStage: window.WindowStage) { 374 // ... 375 console.info('onWindowStageCreate'); 376 let mainWindow: window.Window | undefined = undefined; 377 let subWindow: window.Window | undefined = undefined; 378 let mainWindowId: number = -1; 379 let subWindowId: number = -1; 380 381 try { 382 // 获取应用主窗及ID 383 windowStage.getMainWindow().then((data) => { 384 if (data == null) { 385 console.error('Failed to obtain the main window. Cause: The data is empty'); 386 return; 387 } 388 mainWindow = data; 389 mainWindowId = mainWindow.getWindowProperties().id; 390 console.info('Succeeded in obtaining the main window'); 391 }).catch((err: BusinessError) => { 392 console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`); 393 }); 394 395 // 创建或获取子窗及ID,此时子窗口获焦 396 windowStage.createSubWindow('testSubWindow').then((data) => { 397 if (data == null) { 398 console.error('Failed to obtain the sub window. Cause: The data is empty'); 399 return; 400 } 401 subWindow = data; 402 subWindowId = subWindow.getWindowProperties().id; 403 subWindow.resize(500, 500); 404 subWindow.showWindow(); 405 406 // 监听Window状态,确保已经就绪 407 subWindow.on("windowEvent", (windowEvent) => { 408 if (windowEvent == window.WindowEventType.WINDOW_ACTIVE) { 409 // 切换焦点 410 window.shiftAppWindowFocus(subWindowId, mainWindowId).then(() => { 411 console.info('Succeeded in shifting app window focus'); 412 }).catch((err: BusinessError) => { 413 console.error(`Failed to shift app window focus. Cause code: ${err.code}, message: ${err.message}`); 414 }); 415 } 416 }); 417 }); 418 } catch (exception) { 419 console.error(`Failed to shift app focus. Cause code: ${exception.code}, message: ${exception.message}`); 420 } 421 } 422} 423``` 424 425## window.shiftAppWindowPointerEvent<sup>15+</sup> 426shiftAppWindowPointerEvent(sourceWindowId: number, targetWindowId: number): Promise<void> 427 428该接口仅在[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下生效,用于在同应用内窗口分合场景下,将输入事件从源窗口转移到目标窗口,使用Promise异步回调,针对主窗和子窗生效。 429 430源窗口需要处于鼠标按下状态,否则调用此接口将不生效。输入事件转移后,会向源窗口补发鼠标抬起事件,并且向目标窗口补发鼠标按下事件。 431 432**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 433 434**系统能力:** SystemCapability.Window.SessionManager 435 436**设备行为差异:** 该接口在2in1设备、Tablet设备中可正常调用,在其他设备中返回801错误码。 437 438**参数:** 439 440| 参数名 | 类型 | 必填 | 说明 | 441| -------------- | ------ | ----- | ----------------------- | 442| sourceWindowId | number | 是 | 源窗口id。推荐使用[getWindowProperties()](arkts-apis-window-Window.md#getwindowproperties9)方法获取窗口id属性。 | 443| targetWindowId | number | 是 | 目标窗口id。推荐使用[getWindowProperties()](arkts-apis-window-Window.md#getwindowproperties9)方法获取窗口id属性。 | 444 445**返回值:** 446 447| 类型 | 说明 | 448| ------------------- | ------------------------- | 449| Promise<void> | 无返回结果的Promise对象。 | 450 451**错误码:** 452 453以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 454 455| 错误码ID | 错误信息 | 456| ------- | --------------------------------------------- | 457| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 458| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 459| 1300002 | This window state is abnormal. | 460| 1300003 | This window manager service works abnormally. | 461| 1300004 | Unauthorized operation. | 462 463**示例:** 464 465```ts 466// ets/pages/Index.ets 467import { window } from '@kit.ArkUI'; 468import { BusinessError } from '@kit.BasicServicesKit'; 469 470@Entry 471struct Index { 472 build() { 473 Row() { 474 Column() { 475 Blank('160') 476 .color(Color.Blue) 477 .onTouch((event: TouchEvent) => { 478 if (event.type === TouchType.Down) { 479 try { 480 let sourceWindowId = 1; 481 let targetWindowId = 2; 482 let promise = window.shiftAppWindowPointerEvent(sourceWindowId, targetWindowId); 483 promise.then(() => { 484 console.info('Succeeded in shifting app window pointer event'); 485 }).catch((err: BusinessError) => { 486 console.error(`Failed to shift app window pointer event. Cause code: ${err.code}, message: ${err.message}`); 487 }); 488 } catch (exception) { 489 console.error(`Failed to shift app pointer event. Cause code: ${exception.code}, message: ${exception.message}`); 490 } 491 } 492 }) 493 }.width('100%') 494 }.height('100%').width('100%') 495 } 496} 497``` 498 499## window.shiftAppWindowTouchEvent<sup>20+</sup> 500shiftAppWindowTouchEvent(sourceWindowId: number, targetWindowId: number, fingerId: number): Promise<void> 501 502该接口仅在[自由窗口](../../windowmanager/window-terminology.md#自由窗口)状态下生效,主窗口和子窗口可正常调用,用于将触屏输入事件从源窗口转移到目标窗口。使用Promise异步回调。 503 504源窗口仅在[onTouch](arkui-ts/ts-universal-events-touch.md#ontouch)事件(其中,事件类型必须为TouchType.Down)的回调方法中调用此接口才会有触屏输入事件转移效果,成功调用此接口后,系统会向源窗口补发触屏抬起(touch up)事件,并且向目标窗口补发触屏按下(touch down)事件。 505 506**系统能力:** SystemCapability.Window.SessionManager 507 508**设备行为差异:** 该接口在2in1设备、Tablet设备中可正常调用,在其它设备中返回801错误码。 509 510**参数:** 511 512| 参数名 | 类型 | 必填 | 说明 | 513| -------------- | ------ | ----- | ----------------------- | 514| sourceWindowId | number | 是 | 源窗口id。推荐使用[getWindowProperties()](arkts-apis-window-Window.md#getwindowproperties9)方法获取窗口id属性。该参数应为大于0的整数,小于等于0时会返回错误码1300016。 | 515| targetWindowId | number | 是 | 目标窗口id。推荐使用[getWindowProperties()](arkts-apis-window-Window.md#getwindowproperties9)方法获取窗口id属性。该参数应为大于0的整数,小于等于0时会返回错误码1300016。 | 516| fingerId | number | 是 | 触屏事件的fingerId。推荐使用[touchEvent](arkui-ts/ts-universal-events-touch.md#touchevent对象说明)事件中touches属性获取id。该参数应为大于等于0的整数,小于0时会返回错误码1300016。 | 517 518**返回值:** 519 520| 类型 | 说明 | 521| ------------------- | ------------------------- | 522| Promise<void> | 无返回结果的Promise对象。 | 523 524**错误码:** 525 526以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 527 528| 错误码ID | 错误信息 | 529| ------- | --------------------------------------------- | 530| 801 | Capability not supported. Function shiftAppWindowTouchEvent can not work correctly due to limited device capabilities. | 531| 1300002 | This window state is abnormal. | 532| 1300003 | This window manager service works abnormally. | 533| 1300004 | Unauthorized operation. | 534| 1300016 | Parameter error. Possible cause: 1. Invalid parameter range.| 535 536**示例:** 537 538```ts 539// ets/pages/Index.ets 540import { window } from '@kit.ArkUI'; 541import { BusinessError } from '@kit.BasicServicesKit'; 542 543@Entry 544struct Index { 545 build() { 546 Row() { 547 Column() { 548 Blank('160') 549 .color(Color.Blue) 550 .onTouch((event: TouchEvent) => { 551 // 源窗口触屏事件类型必须为TouchType.Down 552 if (event.type === TouchType.Down) { 553 try { 554 let sourceWindowId = 1; 555 let targetWindowId = 2; 556 let promise = window.shiftAppWindowTouchEvent(sourceWindowId, targetWindowId, event.touches[0].id); 557 promise.then(() => { 558 console.info(`Succeeded in shifting app window touch event`); 559 }).catch((err: BusinessError) => { 560 console.error(`Failed to shift app window touch event. Cause code: ${err.code}, message: ${err.message}`); 561 }); 562 } catch (exception) { 563 console.error(`Failed to shift app touch event. Cause code: ${exception.code}, message: ${exception.message}`); 564 } 565 } 566 }) 567 }.width('100%') 568 }.height('100%').width('100%') 569 } 570} 571``` 572 573## window.getWindowsByCoordinate<sup>14+</sup> 574 575getWindowsByCoordinate(displayId: number, windowNumber?: number, x?: number, y?: number): Promise<Array<Window>> 576 577查询本应用指定坐标下的可见窗口数组,按当前窗口层级排列,层级最高的窗口对应数组下标为0,使用Promise异步回调。 578 579**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 580 581**系统能力:** SystemCapability.Window.SessionManager 582 583**参数:** 584 585| 参数名 | 类型 | 必填 | 说明 | 586| ------ | ---------- |----|---------------------------------------------------------------------------| 587| displayId | number| 是 | 查询窗口所在的displayId,该参数应为整数,可以在窗口属性[WindowProperties](arkts-apis-window-i.md#windowproperties)中获取。 | 588| windowNumber | number| 否 | 查询的窗口数量,该参数应为大于0整数,未设置或小于等于0返回所有满足条件的窗口。 | 589| x | number | 否 | 查询的x坐标,该参数应为非负整数,未设置或小于0返回所有可见窗口。 | 590| y | number| 否 | 查询的y坐标,该参数应为非负整数,未设置或小于0返回所有可见窗口。 | 591 592**返回值:** 593 594| 类型 | 说明 | 595| -------------------------------- |-------------------------| 596| Promise<Array<[Window](arkts-apis-window-Window.md)>> | Promise对象。返回获取到的窗口对象数组。 | 597 598**错误码:** 599 600以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 601 602| 错误码ID | 错误信息 | 603|----------| ------------------------------ | 604| 401 | Parameter error. Possible cause: Incorrect parameter types. | 605| 801 | Capability not supported. Failed to call the API due to limited device capabilities. | 606| 1300003 | This window manager service works abnormally. | 607 608```ts 609import { UIAbility } from '@kit.AbilityKit'; 610import { window } from '@kit.ArkUI'; 611import { BusinessError } from '@kit.BasicServicesKit'; 612 613export default class EntryAbility extends UIAbility { 614 615 onWindowStageCreate(windowStage: window.WindowStage): void { 616 try { 617 let windowClass = windowStage.getMainWindowSync(); 618 let properties = windowClass.getWindowProperties(); 619 window.getWindowsByCoordinate(properties.displayId).then((data) => { 620 console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); 621 for (let window of data) { 622 // do something with window 623 } 624 }).catch((err: BusinessError) => { 625 console.error(`Failed to get window from point. Cause code: ${err.code}, message: ${err.message}`); 626 }); 627 window.getWindowsByCoordinate(properties.displayId, 2, 500, 500).then((data) => { 628 console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); 629 for (let window of data) { 630 // do something with window 631 } 632 }).catch((err: BusinessError) => { 633 console.error(`Failed to get window from point. Cause code: ${err.code}, message: ${err.message}`); 634 }); 635 } catch (exception) { 636 console.error(`Failed to get window from point. Cause code: ${exception.code}, message: ${exception.message}`); 637 } 638 } 639} 640``` 641 642## window.getAllWindowLayoutInfo<sup>15+</sup> 643 644getAllWindowLayoutInfo(displayId: number): Promise<Array<WindowLayoutInfo>> 645 646获取指定屏幕上可见的窗口布局信息数组,按当前窗口层级排列,层级最高的对应数组index为0,使用Promise异步回调。 647 648**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。 649 650**系统能力:** SystemCapability.Window.SessionManager 651 652**参数:** 653 654| 参数名 | 类型 | 必填 | 说明 | 655| ------ | ---------- |----|---------------------------------------------------------------------------| 656| displayId | number| 是 | 需要获取窗口布局信息的displayId,该参数应为整数,且为当前实际存在屏幕的displayId,可以通过窗口属性[WindowProperties](arkts-apis-window-i.md#windowproperties)获取。 | 657 658**返回值:** 659 660| 类型 | 说明 | 661| -------------------------------- |-------------------------| 662| Promise<Array<[WindowLayoutInfo](arkts-apis-window-i.md#windowlayoutinfo15)>> | Promise对象。返回获取到的窗口布局信息对象数组。 | 663 664**错误码:** 665 666以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 667 668| 错误码ID | 错误信息 | 669|----------| ------------------------------ | 670| 401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 671| 801 | Capability not supported. function getAllWindowLayoutInfo can not work correctly due to limited device capabilities. | 672| 1300003 | This window manager service works abnormally. | 673 674```ts 675import { window } from '@kit.ArkUI'; 676import { BusinessError } from '@kit.BasicServicesKit'; 677 678try { 679 let displayId = 0; 680 let promise = window.getAllWindowLayoutInfo(displayId); 681 promise.then((data) => { 682 console.info('Succeeded in obtaining all window layout info. Data: ' + JSON.stringify(data)); 683 }).catch((err: BusinessError) => { 684 console.error(`Failed to obtain all window layout info. Cause code: ${err.code}, message: ${err.message}`); 685 }); 686} catch (exception) { 687 console.error(`Failed to obtain all window layout info. Cause code: ${exception.code}, message: ${exception.message}`); 688} 689``` 690 691## window.getVisibleWindowInfo<sup>18+</sup> 692 693getVisibleWindowInfo(): Promise<Array<WindowInfo>> 694 695获取当前屏幕的可见主窗口(未退至后台的主窗口)信息。使用Promise异步回调。 696 697**系统能力:** SystemCapability.Window.SessionManager 698 699**需要权限:** ohos.permission.VISIBLE_WINDOW_INFO 700 701**返回值:** 702 703| 类型 | 说明 | 704| ------------------- | ----------------------- | 705| Promise<Array<[WindowInfo](arkts-apis-window-i.md#windowinfo18)>> | Promise对象,返回当前可见窗口的相关信息。 | 706 707**错误码:** 708 709以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 710 711| 错误码ID | 错误信息 | 712| ------- | ------------------------------ | 713| 201 | Permission verification failed. The application does not have the permission required to call the API. | 714| 801 | Capability not supported. Function getVisibleWindowInfo can not work correctly due to limited device capabilities. | 715| 1300003 | This window manager service works abnormally. | 716 717**示例:** 718 719```ts 720import { window } from '@kit.ArkUI'; 721import { BusinessError } from '@kit.BasicServicesKit'; 722 723try { 724 let promise = window.getVisibleWindowInfo(); 725 promise.then((data) => { 726 data.forEach(windowInfo=>{ 727 console.info(`left:${windowInfo.rect.left}`); 728 console.info(`top:${windowInfo.rect.top}`); 729 console.info(`width:${windowInfo.rect.width}`); 730 console.info(`height:${windowInfo.rect.height}`); 731 console.info(`windowId:${windowInfo.windowId}`); 732 console.info(`windowStatusType:${windowInfo.windowStatusType}`); 733 console.info(`abilityName:${windowInfo.abilityName}`); 734 console.info(`bundleName:${windowInfo.bundleName}`); 735 console.info(`isFocused:${windowInfo.isFocused}`); 736 }) 737 }).catch((err: BusinessError) => { 738 console.error('Failed to getWindowInfo. Cause: ' + JSON.stringify(err)); 739 }); 740} catch (exception) { 741 console.error(`Failed to get visible window info. Cause code: ${exception.code}, message: ${exception.message}`); 742} 743``` 744 745## window.getGlobalWindowMode<sup>20+</sup> 746 747getGlobalWindowMode(displayId?: number): Promise<number> 748 749获取指定屏幕上生命周期位于前台的窗口对应的窗口模式,使用Promise异步回调。 750 751**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 752 753**系统能力:** SystemCapability.Window.SessionManager 754 755**参数:** 756 757| 参数名 | 类型 | 必填 | 说明 | 758| ------ | ---------- |--------------------|------------------------------------------------------------------------------------| 759| displayId | number| 否 | 可选的屏幕ID,用于获取对应屏幕上的窗口模式信息。该参数应为大于等于0的整数,小于0时会返回错误码1300016,不传或传值为null以及undefined则代表查询所有屏幕。如果指定的屏幕不存在,返回值为0,推荐使用[getWindowProperties()](arkts-apis-window-Window.md#getwindowproperties9)方法获取窗口所在屏幕id属性。 | 760 761**返回值:** 762 763| 类型 | 说明 | 764| -------------------------------- |-------------------------| 765| Promise<number> | Promise对象。返回获取到的窗口模式。每一个二进制位代表一种窗口模式,当前支持的窗口模式见[GlobalWindowMode](arkts-apis-window-e.md#globalwindowmode20),返回值为对应窗口模式值按位进行或运算的结果,比如,当前屏幕上存在全屏窗口、悬浮窗和画中画三种窗口,则返回值为`0b1\|0b100\|0b1000 = 13`。| 766 767**错误码:** 768 769以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 770 771| 错误码ID | 错误信息 | 772|----------| ------------------------------ | 773| 801 | Capability not supported. function getGlobalWindowMode can not work correctly due to limited device capabilities. | 774| 1300003 | This window manager service works abnormally. | 775| 1300016 | Parameter error. Possible cause: 1. Invalid parameter range. | 776 777**示例:** 778```ts 779import { window } from '@kit.ArkUI'; 780import { BusinessError } from '@kit.BasicServicesKit'; 781 782try { 783 let displayId = 0; 784 let promise = window.getGlobalWindowMode(displayId); 785 promise.then((data) => { 786 console.info(`Succeeded in obtaining global window mode. Data: ${data}`); 787 }).catch((err: BusinessError) => { 788 console.error(`Failed to obtain global window mode. Cause code: ${err.code}, message: ${err.message}`); 789 }); 790} catch (exception) { 791 console.error(`Failed to obtain global window mode. Cause code: ${exception.code}, message: ${exception.message}`); 792} 793``` 794 795## window.setStartWindowBackgroundColor<sup>20+</sup> 796 797setStartWindowBackgroundColor(moduleName: string, abilityName: string, color: ColorMetrics): Promise<void> 798 799设置同应用内指定mouduleName、abilityName对应UIAbility的启动页背景色,使用Promise异步回调。 800 801该接口对同应用的所有进程生效,例如多实例或应用分身场景。 802 803**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 804 805**系统能力:** SystemCapability.Window.SessionManager 806 807**参数:** 808 809| 参数名 | 类型 | 必填 | 说明 | 810| -------- | ----------------------------- | ---- | -------------------------------------------------------- | 811| moduleName | string | 是 | 需要设置的UIAbility所属module的名字,moduleName的长度范围为0-200,仅支持设置当前同一应用包名内的moduleName。 | 812| abilityName | string | 是 | 需要设置的UIAbility名字,abilityName的长度范围为0-200,仅支持设置当前同一应用包名内的abilityName。 | 813| color | [ColorMetrics](js-apis-arkui-graphics.md#colormetrics12) | 是 | 设置的启动页背景色。 | 814 815**返回值:** 816 817| 类型 | 说明 | 818| ------------------- | ------------------------ | 819| Promise<void> | 无返回结果的Promise对象。 | 820 821**错误码:** 822 823以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[窗口错误码](errorcode-window.md)。 824 825| 错误码ID | 错误信息 | 826| ------- | -------------------------------------------- | 827| 801 | Capability not supported.function setStartWindowBackgroundColor can not to work correctly due to limited device capabilities. | 828| 1300003 | This window manager service works abnormally. | 829| 1300016 | Parameter error. Possible cause: 1. Invalid parameter range. | 830 831**示例:** 832 833```ts 834import { BusinessError } from '@kit.BasicServicesKit'; 835import { ColorMetrics, window } from '@kit.ArkUI'; 836 837try { 838 let promise = window.setStartWindowBackgroundColor("entry", "EntryAbility", ColorMetrics.numeric(0xff000000)); 839 promise.then(() => { 840 console.log('Succeeded in setting the starting window color.'); 841 }).catch((err: BusinessError) => { 842 console.error(`Failed to set the starting window color. Cause code: ${err.code}, message: ${err.message}`); 843 }); 844} catch (exception) { 845 console.error(`Failed to set the starting window color. Cause code: ${exception.code}, message: ${exception.message}`); 846} 847``` 848 849## window.create<sup>(deprecated)</sup> 850 851create(id: string, type: WindowType, callback: AsyncCallback<Window>): void 852 853创建子窗口,使用callback异步回调。 854 855子窗口创建后默认是[沉浸式布局](../../windowmanager/window-terminology.md#沉浸式布局)。 856 857> **说明:** 858> 859> 从API version 7开始支持,从API version 9开始废弃,推荐使用[createWindow()](#windowcreatewindow9)。 860 861**模型约束:** 此接口仅可在FA模型下使用。 862 863**系统能力:** SystemCapability.WindowManager.WindowManager.Core 864 865**参数:** 866 867| 参数名 | 类型 | 必填 | 说明 | 868| -------- | -------------------------------------- | ---- | ------------------------------------ | 869| id | string | 是 | 窗口名字,即[Configuration](arkts-apis-window-i.md#configuration9)中的name。| 870| type | [WindowType](arkts-apis-window-e.md#windowtype7) | 是 | 窗口类型。 | 871| callback | AsyncCallback<[Window](arkts-apis-window-Window.md)> | 是 | 回调函数。返回当前创建的子窗口对象。 | 872 873 874**示例:** 875 876```ts 877import { BusinessError } from '@kit.BasicServicesKit'; 878 879let windowClass: window.Window | undefined = undefined; 880window.create('test', window.WindowType.TYPE_APP, (err: BusinessError, data) => { 881 const errCode: number = err.code; 882 if (errCode) { 883 console.error(`Failed to create the subWindow. Cause code: ${err.code}, message: ${err.message}`); 884 return; 885 } 886 windowClass = data; 887 console.info('Succeeded in creating the subWindow. Data: ' + JSON.stringify(data)); 888}); 889``` 890 891## window.create<sup>(deprecated)</sup> 892 893create(id: string, type: WindowType): Promise<Window> 894 895创建子窗口,使用Promise异步回调。 896 897子窗口创建后默认是[沉浸式布局](../../windowmanager/window-terminology.md#沉浸式布局)。 898 899> **说明:** 900> 901> 从API version 7开始支持,从API version 9开始废弃,推荐使用[createWindow()](#windowcreatewindow9-1)。 902 903**模型约束:** 此接口仅可在FA模型下使用。 904 905**系统能力:** SystemCapability.WindowManager.WindowManager.Core 906 907**参数:** 908 909| 参数名 | 类型 | 必填 | 说明 | 910| ------ | ------------------------- | ---- | ---------- | 911| id | string | 是 | 窗口名字,即[Configuration](arkts-apis-window-i.md#configuration9)中的name。 | 912| type | [WindowType](arkts-apis-window-e.md#windowtype7) | 是 | 窗口类型。 | 913 914**返回值:** 915 916| 类型 | 说明 | 917| -------------------------------- | --------------------------------------- | 918| Promise<[Window](arkts-apis-window-Window.md)> | Promise对象。返回当前创建的子窗口对象。 | 919 920 921**示例:** 922 923```ts 924import { BusinessError } from '@kit.BasicServicesKit'; 925 926let windowClass: window.Window | undefined = undefined; 927let promise = window.create('test', window.WindowType.TYPE_APP); 928promise.then((data) => { 929 windowClass = data; 930 console.info('Succeeded in creating the subWindow. Data: ' + JSON.stringify(data)); 931}).catch((err: BusinessError) => { 932 console.error(`Failed to create the subWindow. Cause code: ${err.code}, message: ${err.message}`); 933}); 934``` 935 936## window.create<sup>(deprecated)</sup> 937 938create(ctx: BaseContext, id: string, type: WindowType, callback: AsyncCallback<Window>): void 939 940创建系统窗口,使用callback异步回调。 941 942> **说明:** 943> 944> 从API version 7开始支持,从API version 9开始废弃,推荐使用[createWindow()](#windowcreatewindow9)。 945 946**系统能力:** SystemCapability.WindowManager.WindowManager.Core 947 948**参数:** 949 950| 参数名 | 类型 | 必填 | 说明 | 951| -------- | ------------------------------------------------------- | ---- | ------------------------------------ | 952| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前应用上下文信息。 | 953| id | string | 是 | 窗口名字,即[Configuration](arkts-apis-window-i.md#configuration9)中的name。 | 954| type | [WindowType](arkts-apis-window-e.md#windowtype7) | 是 | 窗口类型。 | 955| callback | AsyncCallback<[Window](arkts-apis-window-Window.md)> | 是 | 回调函数。返回当前创建的子窗口对象。 | 956 957 958**示例:** 959 960```ts 961import { BusinessError } from '@kit.BasicServicesKit'; 962 963let windowClass: window.Window | undefined = undefined; 964window.create(globalThis.getContext(), 'test', window.WindowType.TYPE_SYSTEM_ALERT, (err: BusinessError, data) => { 965 const errCode: number = err.code; 966 if (errCode) { 967 console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`); 968 return; 969 } 970 windowClass = data; 971 console.info('Succeeded in creating the window. Data: ' + JSON.stringify(data)); 972 windowClass.resetSize(500, 1000); 973}); 974``` 975 976## window.create<sup>(deprecated)</sup> 977 978create(ctx: BaseContext, id: string, type: WindowType): Promise<Window> 979 980创建系统窗口,使用Promise异步回调。 981 982> **说明:** 983> 984> 从API version 7开始支持,从API version 9开始废弃,推荐使用[createWindow()](#windowcreatewindow9-1)。 985 986**系统能力:** SystemCapability.WindowManager.WindowManager.Core 987 988**参数:** 989 990| 参数名 | 类型 | 必填 | 说明 | 991| ------ | ------------------------- | ---- | ------------------------------------------------------------ | 992| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前应用上下文信息。 | 993| id | string | 是 | 窗口名字,即[Configuration](arkts-apis-window-i.md#configuration9)中的name。 | 994| type | [WindowType](arkts-apis-window-e.md#windowtype7) | 是 | 窗口类型。 | 995 996**返回值:** 997 998| 类型 | 说明 | 999| -------------------------------- | --------------------------------------- | 1000| Promise<[Window](arkts-apis-window-Window.md)> | Promise对象。返回当前创建的子窗口对象。 | 1001 1002 1003**示例:** 1004 1005```ts 1006import { BusinessError } from '@kit.BasicServicesKit'; 1007 1008let windowClass: window.Window | undefined = undefined; 1009let promise = window.create(globalThis.getContext(), 'test', window.WindowType.TYPE_SYSTEM_ALERT); 1010promise.then((data) => { 1011 windowClass = data; 1012 console.info('Succeeded in creating the window. Data:' + JSON.stringify(data)); 1013}).catch((err: BusinessError) => { 1014 console.error(`Failed to create the Window. Cause code: ${err.code}, message: ${err.message}`); 1015}); 1016``` 1017 1018## window.find<sup>(deprecated)</sup> 1019 1020find(id: string, callback: AsyncCallback<Window>): void 1021 1022查找id所对应的窗口,使用callback异步回调。 1023 1024> **说明:** 1025> 1026> 从API version 7开始支持,从API version 9开始废弃,推荐使用[findWindow()](#windowfindwindow9)。 1027 1028**系统能力:** SystemCapability.WindowManager.WindowManager.Core 1029 1030**参数:** 1031 1032| 参数名 | 类型 | 必填 | 说明 | 1033| -------- | -------------------------------------- | ---- | ------------------------------------ | 1034| id | string | 是 | 窗口名字,即[Configuration](arkts-apis-window-i.md#configuration9)中的name。 | 1035| callback | AsyncCallback<[Window](arkts-apis-window-Window.md)> | 是 | 回调函数。返回当前查找到的窗口对象。 | 1036 1037**示例:** 1038 1039```ts 1040import { BusinessError } from '@kit.BasicServicesKit'; 1041 1042let windowClass: window.Window | undefined = undefined; 1043window.find('test', (err: BusinessError, data) => { 1044 const errCode: number = err.code; 1045 if (errCode) { 1046 console.error(`Failed to find the Window. Cause code: ${err.code}, message: ${err.message}`); 1047 return; 1048 } 1049 windowClass = data; 1050 console.info('Succeeded in finding the window. Data: ' + JSON.stringify(data)); 1051}); 1052``` 1053 1054## window.find<sup>(deprecated)</sup> 1055 1056find(id: string): Promise<Window> 1057 1058查找id所对应的窗口,使用Promise异步回调。 1059 1060> **说明:** 1061> 1062> 从API version 7开始支持,从API version 9开始废弃,推荐使用[findWindow()](#windowfindwindow9)。 1063 1064**系统能力:** SystemCapability.WindowManager.WindowManager.Core 1065 1066**参数:** 1067 1068| 参数名 | 类型 | 必填 | 说明 | 1069| ------ | ------ | ---- | -------- | 1070| id | string | 是 | 窗口名字,即[Configuration](arkts-apis-window-i.md#configuration9)中的name。 | 1071 1072**返回值:** 1073 1074| 类型 | 说明 | 1075| -------------------------------- | ------------------------------------- | 1076| Promise<[Window](arkts-apis-window-Window.md)> | Promise对象。返回当前查找的窗口对象。 | 1077 1078**示例:** 1079 1080```ts 1081import { BusinessError } from '@kit.BasicServicesKit'; 1082 1083let windowClass: window.Window | undefined = undefined; 1084let promise = window.find('test'); 1085promise.then((data) => { 1086 windowClass = data; 1087 console.info('Succeeded in finding the window. Data: ' + JSON.stringify(data)); 1088}).catch((err: BusinessError) => { 1089 console.error(`Failed to find the Window. Cause code: ${err.code}, message: ${err.message}`); 1090}); 1091``` 1092 1093## window.getTopWindow<sup>(deprecated)</sup> 1094 1095getTopWindow(callback: AsyncCallback<Window>): void 1096 1097获取当前应用内最后显示的窗口,使用callback异步回调。 1098 1099> **说明:** 1100> 1101> 从API version 6开始支持,从API version 9开始废弃,推荐使用[getLastWindow()](#windowgetlastwindow9)。 1102 1103**模型约束:** 此接口仅可在FA模型下使用。 1104 1105**系统能力:** SystemCapability.WindowManager.WindowManager.Core 1106 1107**参数:** 1108 1109| 参数名 | 类型 | 必填 | 说明 | 1110| -------- | -------------------------------------- | ---- | -------------------------------------------- | 1111| callback | AsyncCallback<[Window](arkts-apis-window-Window.md)> | 是 | 回调函数。返回当前应用内最后显示的窗口对象。 | 1112 1113**示例:** 1114 1115```ts 1116import { BusinessError } from '@kit.BasicServicesKit'; 1117 1118let windowClass: window.Window | undefined = undefined; 1119window.getTopWindow((err: BusinessError, data) => { 1120 const errCode: number = err.code; 1121 if (errCode) { 1122 console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`); 1123 return; 1124 } 1125 windowClass = data; 1126 console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); 1127}); 1128``` 1129 1130## window.getTopWindow<sup>(deprecated)</sup> 1131 1132getTopWindow(): Promise<Window> 1133 1134获取当前应用内最后显示的窗口,使用Promise异步回调。 1135 1136> **说明:** 1137> 1138> 从API version 6开始支持,从API version 9开始废弃,推荐使用[getLastWindow()](#windowgetlastwindow9-1)。 1139 1140**模型约束:** 此接口仅可在FA模型下使用。 1141 1142**系统能力:** SystemCapability.WindowManager.WindowManager.Core 1143 1144**返回值:** 1145 1146| 类型 | 说明 | 1147| -------------------------------- | ----------------------------------------------- | 1148| Promise<[Window](arkts-apis-window-Window.md)> | Promise对象。返回当前应用内最后显示的窗口对象。 | 1149 1150**示例:** 1151 1152```ts 1153import { BusinessError } from '@kit.BasicServicesKit'; 1154 1155let windowClass: window.Window | undefined = undefined; 1156let promise = window.getTopWindow(); 1157promise.then((data)=> { 1158 windowClass = data; 1159 console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); 1160}).catch((err: BusinessError)=>{ 1161 console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`); 1162}); 1163``` 1164 1165## window.getTopWindow<sup>(deprecated)</sup> 1166 1167getTopWindow(ctx: BaseContext, callback: AsyncCallback<Window>): void 1168 1169获取当前应用内最后显示的窗口,使用callback异步回调。 1170 1171> **说明:** 1172> 1173> 从API version 8开始支持,从API version 9开始废弃,推荐使用[getLastWindow()](#windowgetlastwindow9)。 1174 1175**系统能力:** SystemCapability.WindowManager.WindowManager.Core 1176 1177**参数:** 1178 1179| 参数名 | 类型 | 必填 | 说明 | 1180| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 1181| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前应用上下文信息。 | 1182| callback | AsyncCallback<[Window](arkts-apis-window-Window.md)> | 是 | 回调函数。返回当前应用内最后显示的窗口对象。 | 1183 1184**示例:** 1185 1186```ts 1187// EntryAbility.ets 1188import { UIAbility } from '@kit.AbilityKit'; 1189import { BusinessError } from '@kit.BasicServicesKit'; 1190 1191export default class EntryAbility extends UIAbility { 1192 onWindowStageCreate(windowStage:window.WindowStage){ 1193 console.info('onWindowStageCreate'); 1194 let windowClass: window.Window | undefined = undefined; 1195 try { 1196 window.getTopWindow(this.context, (err: BusinessError, data) => { 1197 const errCode: number = err.code; 1198 if(errCode){ 1199 console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`); 1200 return ; 1201 } 1202 windowClass = data; 1203 console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); 1204 }); 1205 } catch(error){ 1206 console.error(`Failed to obtain the top window. Cause code: ${error.code}, message: ${error.message}`); 1207 } 1208 } 1209} 1210``` 1211 1212## window.getTopWindow<sup>(deprecated)</sup> 1213 1214getTopWindow(ctx: BaseContext): Promise<Window> 1215 1216获取当前应用内最后显示的窗口,使用Promise异步回调。 1217 1218> **说明:** 1219> 1220> 从API version 8开始支持,从API version 9开始废弃,推荐使用[getLastWindow()](#windowgetlastwindow9-1)。 1221 1222**系统能力:** SystemCapability.WindowManager.WindowManager.Core 1223 1224**参数:** 1225 1226| 参数名 | 类型 | 必填 | 说明 | 1227| ------ | ----------- | ---- | ------------------------------------------------------------ | 1228| ctx | [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md) | 是 | 当前应用上下文信息。 | 1229 1230**返回值:** 1231 1232| 类型 | 说明 | 1233| -------------------------------- | ----------------------------------------------- | 1234| Promise<[Window](arkts-apis-window-Window.md)> | Promise对象。返回当前应用内最后显示的窗口对象。 | 1235 1236**示例:** 1237 1238```ts 1239// EntryAbility.ets 1240import { UIAbility } from '@kit.AbilityKit'; 1241import { BusinessError } from '@kit.BasicServicesKit'; 1242 1243export default class EntryAbility extends UIAbility { 1244 onWindowStageCreate(windowStage:window.WindowStage) { 1245 console.info('onWindowStageCreate'); 1246 let windowClass: window.Window | undefined = undefined; 1247 let promise = window.getTopWindow(this.context); 1248 promise.then((data) => { 1249 windowClass = data; 1250 console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); 1251 }).catch((error: BusinessError) => { 1252 console.error(`Failed to obtain the top window. Cause code: ${error.code}, message: ${error.message}`); 1253 }); 1254 } 1255} 1256```