1# 管理应用窗口(Stage模型) 2 3 4## 基本概念 5 6- 窗口沉浸式能力:指对状态栏、导航栏等系统窗口进行控制,减少状态栏导航栏等系统界面的突兀感,从而使用户获得最佳体验的能力。 7 沉浸式能力只在应用主窗口作为全屏窗口时生效。通常情况下,应用子窗口(弹窗、悬浮窗口等辅助窗口)和处于自由窗口下的应用主窗口无法使用沉浸式能力。 8 9- 悬浮窗:全局悬浮窗口是一种特殊的应用窗口,具备在应用主窗口和对应Ability退至后台后仍然可以在前台显示的能力。 10 悬浮窗口可以用于应用退至后台后,使用小窗继续播放视频,或者为特定的应用创建悬浮球等快速入口。应用在创建悬浮窗口前,需要申请对应的权限。 11 12 13## 场景介绍 14 15在`Stage`模型下,管理应用窗口的典型场景有: 16 17- 设置应用主窗口属性及目标页面 18 19- 设置应用子窗口属性及目标页面 20 21- 体验窗口沉浸式能力 22 23- 设置悬浮窗 24 25- 监听窗口不可交互与可交互事件 26 27以下分别介绍具体开发方式。 28 29## 接口说明 30 31上述场景涉及的常用接口如下表所示。更多API说明请参见[API参考](../reference/apis-arkui/js-apis-window.md)。 32 33| 实例名 | 接口名 | 描述 | 34| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 35| WindowStage | getMainWindow(callback: AsyncCallback<Window>): void | 获取`WindowStage`实例下的主窗口。<br/>此接口仅可在`Stage`模型下使用。 | 36| WindowStage | loadContent(path: string, callback: AsyncCallback<void>): void | 为当前`WindowStage`的主窗口加载具体页面。<br>其中path为要加载到窗口中的页面内容的路径,该路径需添加到工程的main_pages.json文件中。<br/>此接口仅可在`Stage`模型下使用。 | 37| WindowStage | createSubWindow(name: string, callback: AsyncCallback<Window>): void | 创建子窗口。<br/>此接口仅可在`Stage`模型下使用。 | 38| WindowStage | on(type: 'windowStageEvent', callback: Callback<WindowStageEventType>): void | 开启WindowStage生命周期变化的监听。<br/>此接口仅可在`Stage`模型下使用。 | 39| window静态方法 | createWindow(config: Configuration, callback: AsyncCallback\<Window>): void | 创建子窗口或者系统窗口。<br/>-`config`:创建窗口时的参数。 | 40| Window | setUIContent(path: string, callback: AsyncCallback<void>): void | 根据当前工程中某个页面的路径为窗口加载具体的页面内容。<br>其中path为要加载到窗口中的页面内容的路径,在Stage模型下该路径需添加到工程的main_pages.json文件中。 | 41| Window | setWindowBrightness(brightness: number, callback: AsyncCallback<void>): void | 设置屏幕亮度值。 | 42| Window | setWindowTouchable(isTouchable: boolean, callback: AsyncCallback<void>): void | 设置窗口是否为可触状态。 | 43| Window | moveWindowTo(x: number, y: number, callback: AsyncCallback<void>): void | 移动当前窗口位置。 | 44| Window | resize(width: number, height: number, callback: AsyncCallback<void>): void | 改变当前窗口大小。 | 45| Window | setWindowLayoutFullScreen(isLayoutFullScreen: boolean): Promise<void> | 设置窗口布局是否为全屏布局。 | 46| Window | setWindowSystemBarEnable(names: Array<'status'\|'navigation'>): Promise<void> | 设置导航栏、状态栏是否显示。 | 47| Window | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties): Promise<void> | 设置窗口内导航栏、状态栏属性。<br/>`systemBarProperties`:导航栏、状态栏的属性集合。 | 48| Window | showWindow(callback: AsyncCallback\<void>): void | 显示当前窗口。 | 49| Window | on(type: 'touchOutside', callback: Callback<void>): void | 开启本窗口区域外的点击事件的监听。 | 50| Window | destroyWindow(callback: AsyncCallback<void>): void | 销毁当前窗口。 | 51 52 53## 设置应用主窗口 54 55在`Stage`模型下,应用主窗口由`UIAbility`创建并维护生命周期。在`UIAbility`的`onWindowStageCreate`回调中,通过`WindowStage`获取应用主窗口,即可对其进行属性设置等操作。还可以在应用配置文件中设置应用主窗口的属性,如最大窗口宽度maxWindowWidth等,详见[module.json5配置文件中的abilities标签](../quick-start/module-configuration-file.md#abilities标签)。 56 57### 开发步骤 58 591. 获取应用主窗口。 60 61 通过`getMainWindow`接口获取应用主窗口。 62 632. 设置主窗口属性。 64 65 可设置主窗口的背景色、亮度值、是否可触等多个属性,开发者可根据需要选择对应的接口。本示例以设置“是否可触”属性为例。 66 673. 为主窗口加载对应的目标页面。 68 69 通过`loadContent`接口加载主窗口的目标页面。 70 71```ts 72import { UIAbility } from '@kit.AbilityKit'; 73import { window } from '@kit.ArkUI'; 74import { BusinessError } from '@kit.BasicServicesKit'; 75 76export default class EntryAbility extends UIAbility { 77 onWindowStageCreate(windowStage: window.WindowStage) { 78 // 1.获取应用主窗口。 79 let windowClass: window.Window | null = null; 80 windowStage.getMainWindow((err: BusinessError, data) => { 81 let errCode: number = err.code; 82 if (errCode) { 83 console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 84 return; 85 } 86 windowClass = data; 87 console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 88 // 2.设置主窗口属性。以设置"是否可触"属性为例。 89 let isTouchable: boolean = true; 90 windowClass.setWindowTouchable(isTouchable, (err: BusinessError) => { 91 let errCode: number = err.code; 92 if (errCode) { 93 console.error('Failed to set the window to be touchable. Cause:' + JSON.stringify(err)); 94 return; 95 } 96 console.info('Succeeded in setting the window to be touchable.'); 97 }) 98 }) 99 // 3.为主窗口加载对应的目标页面。 100 windowStage.loadContent("pages/page2", (err: BusinessError) => { 101 let errCode: number = err.code; 102 if (errCode) { 103 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 104 return; 105 } 106 console.info('Succeeded in loading the content.'); 107 }); 108 } 109}; 110``` 111 112## 设置应用子窗口 113 114开发者可以按需创建应用子窗口,如弹窗等,并对其进行属性设置等操作。 115 116### 开发步骤 117 1181. 创建应用子窗口。 119 120 通过`createSubWindow`接口创建应用子窗口。 121 1222. 设置子窗口属性。 123 124 子窗口创建成功后,可以改变其大小、位置等,还可以根据应用需要设置窗口背景色、亮度等属性。 125 1263. 加载显示子窗口的具体内容。 127 128 通过`setUIContent`和`showWindow`接口加载显示子窗口的具体内容。 129 1304. 销毁子窗口。 131 132 当不再需要某些子窗口时,可根据具体实现逻辑,使用`destroyWindow`接口销毁子窗口。 133 134直接在onWindowStageCreate里面创建子窗口的整体示例代码如下: 135 136```ts 137import { UIAbility } from '@kit.AbilityKit'; 138import { window } from '@kit.ArkUI'; 139import { BusinessError } from '@kit.BasicServicesKit'; 140 141let windowStage_: window.WindowStage | null = null; 142let sub_windowClass: window.Window | null = null; 143 144export default class EntryAbility extends UIAbility { 145 showSubWindow() { 146 // 1.创建应用子窗口。 147 if (windowStage_ == null) { 148 console.error('Failed to create the subwindow. Cause: windowStage_ is null'); 149 } 150 else { 151 windowStage_.createSubWindow("mySubWindow", (err: BusinessError, data) => { 152 let errCode: number = err.code; 153 if (errCode) { 154 console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err)); 155 return; 156 } 157 sub_windowClass = data; 158 console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); 159 // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。 160 sub_windowClass.moveWindowTo(300, 300, (err: BusinessError) => { 161 let errCode: number = err.code; 162 if (errCode) { 163 console.error('Failed to move the window. Cause:' + JSON.stringify(err)); 164 return; 165 } 166 console.info('Succeeded in moving the window.'); 167 }); 168 sub_windowClass.resize(500, 500, (err: BusinessError) => { 169 let errCode: number = err.code; 170 if (errCode) { 171 console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); 172 return; 173 } 174 console.info('Succeeded in changing the window size.'); 175 }); 176 // 3.为子窗口加载对应的目标页面。 177 sub_windowClass.setUIContent("pages/page3", (err: BusinessError) => { 178 let errCode: number = err.code; 179 if (errCode) { 180 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 181 return; 182 } 183 console.info('Succeeded in loading the content.'); 184 // 3.显示子窗口。 185 (sub_windowClass as window.Window).showWindow((err: BusinessError) => { 186 let errCode: number = err.code; 187 if (errCode) { 188 console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); 189 return; 190 } 191 console.info('Succeeded in showing the window.'); 192 }); 193 }); 194 }) 195 } 196 } 197 198 destroySubWindow() { 199 // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。 200 (sub_windowClass as window.Window).destroyWindow((err: BusinessError) => { 201 let errCode: number = err.code; 202 if (errCode) { 203 console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); 204 return; 205 } 206 console.info('Succeeded in destroying the window.'); 207 }); 208 } 209 210 onWindowStageCreate(windowStage: window.WindowStage) { 211 windowStage_ = windowStage; 212 // 开发者可以在适当的时机,如主窗口上按钮点击事件等,创建子窗口。并不一定需要在onWindowStageCreate调用,这里仅作展示 213 this.showSubWindow(); 214 } 215 216 onWindowStageDestroy() { 217 // 开发者可以在适当的时机,如子窗口上点击关闭按钮等,销毁子窗口。并不一定需要在onWindowStageDestroy调用,这里仅作展示 218 this.destroySubWindow(); 219 } 220}; 221``` 222 223另外,也可以在某个page页面通过点击按钮创建子窗口,整体示例代码如下: 224 225```ts 226// EntryAbility.ets 227onWindowStageCreate(windowStage: window.WindowStage) { 228 windowStage.loadContent('pages/Index', (err) => { 229 if (err.code) { 230 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 231 return; 232 } 233 console.info('Succeeded in loading the content.'); 234 }) 235 236 // 给Index页面传递windowStage 237 AppStorage.setOrCreate('windowStage', windowStage); 238} 239``` 240 241```ts 242// Index.ets 243import { window } from '@kit.ArkUI'; 244import { BusinessError } from '@kit.BasicServicesKit'; 245 246let windowStage_: window.WindowStage | undefined = undefined; 247let sub_windowClass: window.Window | undefined = undefined; 248@Entry 249@Component 250struct Index { 251 @State message: string = 'Hello World'; 252 private CreateSubWindow(){ 253 // 获取windowStage 254 windowStage_ = AppStorage.get('windowStage'); 255 // 1.创建应用子窗口。 256 if (windowStage_ == null) { 257 console.error('Failed to create the subwindow. Cause: windowStage_ is null'); 258 } 259 else { 260 windowStage_.createSubWindow("mySubWindow", (err: BusinessError, data) => { 261 let errCode: number = err.code; 262 if (errCode) { 263 console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err)); 264 return; 265 } 266 sub_windowClass = data; 267 console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); 268 // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。 269 sub_windowClass.moveWindowTo(300, 300, (err: BusinessError) => { 270 let errCode: number = err.code; 271 if (errCode) { 272 console.error('Failed to move the window. Cause:' + JSON.stringify(err)); 273 return; 274 } 275 console.info('Succeeded in moving the window.'); 276 }); 277 sub_windowClass.resize(500, 500, (err: BusinessError) => { 278 let errCode: number = err.code; 279 if (errCode) { 280 console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); 281 return; 282 } 283 console.info('Succeeded in changing the window size.'); 284 }); 285 // 3.为子窗口加载对应的目标页面。 286 sub_windowClass.setUIContent("pages/subWindow", (err: BusinessError) => { 287 let errCode: number = err.code; 288 if (errCode) { 289 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 290 return; 291 } 292 console.info('Succeeded in loading the content.'); 293 // 3.显示子窗口。 294 (sub_windowClass as window.Window).showWindow((err: BusinessError) => { 295 let errCode: number = err.code; 296 if (errCode) { 297 console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); 298 return; 299 } 300 console.info('Succeeded in showing the window.'); 301 }); 302 }); 303 }) 304 } 305 } 306 private destroySubWindow(){ 307 // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。 308 (sub_windowClass as window.Window).destroyWindow((err: BusinessError) => { 309 let errCode: number = err.code; 310 if (errCode) { 311 console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); 312 return; 313 } 314 console.info('Succeeded in destroying the window.'); 315 }); 316 } 317 build() { 318 Row() { 319 Column() { 320 Text(this.message) 321 .fontSize(50) 322 .fontWeight(FontWeight.Bold) 323 Button(){ 324 Text('CreateSubWindow') 325 .fontSize(24) 326 .fontWeight(FontWeight.Normal) 327 }.width(220).height(68) 328 .margin({left:10, top:60}) 329 .onClick(() => { 330 this.CreateSubWindow() 331 }) 332 Button(){ 333 Text('destroySubWindow') 334 .fontSize(24) 335 .fontWeight(FontWeight.Normal) 336 }.width(220).height(68) 337 .margin({left:10, top:60}) 338 .onClick(() => { 339 this.destroySubWindow() 340 }) 341 } 342 .width('100%') 343 } 344 .height('100%') 345 } 346} 347``` 348 349```ts 350// subWindow.ets 351@Entry 352@Component 353struct SubWindow { 354 @State message: string = 'Hello subWindow'; 355 build() { 356 Row() { 357 Column() { 358 Text(this.message) 359 .fontSize(50) 360 .fontWeight(FontWeight.Bold) 361 } 362 .width('100%') 363 } 364 .height('100%') 365 } 366} 367``` 368 369## 体验窗口沉浸式能力 370 371在看视频、玩游戏等场景下,用户往往希望隐藏状态栏、导航栏等不必要的系统窗口,从而获得更佳的沉浸式体验。此时可以借助窗口沉浸式能力(窗口沉浸式能力都是针对应用主窗口而言的),达到预期效果。从API version 10开始,沉浸式窗口默认配置为全屏大小并由组件模块控制布局,状态栏、导航栏背景颜色为透明,文字颜色为黑色;应用窗口调用`setWindowLayoutFullScreen`接口,设置为true表示由组件模块控制忽略状态栏、导航栏的沉浸式全屏布局,设置为false表示由组件模块控制避让状态栏、导航栏的非沉浸式全屏布局。 372 373> **说明:** 374> 375> 当前沉浸式界面开发仅支持window级别的配置,暂不支持Page级别的配置。若有Page级别切换的需要,可以在页面生命周期开始,例如onPageShow中设置沉浸模式,然后在页面退出,例如onPageHide中恢复默认设置来实现。 376 377### 开发步骤 378 3791. 获取应用主窗口。 380 381 通过`getMainWindow`接口获取应用主窗口。 382 3832. 实现沉浸式效果。有以下两种方式: 384 385 - 方式一:应用主窗口为全屏窗口时,调用`setWindowSystemBarEnable`接口,设置导航栏、状态栏不显示,从而达到沉浸式效果。 386 387 - 方式二:调用`setWindowLayoutFullScreen`接口,设置应用主窗口为全屏布局;然后调用`setWindowSystemBarProperties`接口,设置导航栏、状态栏的透明度、背景/文字颜色以及高亮图标等属性,使之保持与主窗口显示协调一致,从而达到沉浸式效果。 388 3893. 加载显示沉浸式窗口的具体内容。 390 391 通过`loadContent`接口加载沉浸式窗口的具体内容。 392 393```ts 394import { UIAbility } from '@kit.AbilityKit'; 395import { window } from '@kit.ArkUI'; 396import { BusinessError } from '@kit.BasicServicesKit'; 397 398export default class EntryAbility extends UIAbility { 399 onWindowStageCreate(windowStage: window.WindowStage) { 400 // 1.获取应用主窗口。 401 let windowClass: window.Window | null = null; 402 windowStage.getMainWindow((err: BusinessError, data) => { 403 let errCode: number = err.code; 404 if (errCode) { 405 console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 406 return; 407 } 408 windowClass = data; 409 console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 410 411 // 2.实现沉浸式效果。方式一:设置导航栏、状态栏不显示。 412 let names: Array<'status' | 'navigation'> = []; 413 windowClass.setWindowSystemBarEnable(names) 414 .then(() => { 415 console.info('Succeeded in setting the system bar to be visible.'); 416 }) 417 .catch((err: BusinessError) => { 418 console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); 419 }); 420 // 2.实现沉浸式效果。方式二:设置窗口为全屏布局,配合设置导航栏、状态栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。 421 let isLayoutFullScreen = true; 422 windowClass.setWindowLayoutFullScreen(isLayoutFullScreen) 423 .then(() => { 424 console.info('Succeeded in setting the window layout to full-screen mode.'); 425 }) 426 .catch((err: BusinessError) => { 427 console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err)); 428 }); 429 let sysBarProps: window.SystemBarProperties = { 430 statusBarColor: '#ff00ff', 431 navigationBarColor: '#00ff00', 432 // 以下两个属性从API Version 8开始支持 433 statusBarContentColor: '#ffffff', 434 navigationBarContentColor: '#ffffff' 435 }; 436 windowClass.setWindowSystemBarProperties(sysBarProps) 437 .then(() => { 438 console.info('Succeeded in setting the system bar properties.'); 439 }) 440 .catch((err: BusinessError) => { 441 console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); 442 }); 443 }) 444 // 3.为沉浸式窗口加载对应的目标页面。 445 windowStage.loadContent("pages/page2", (err: BusinessError) => { 446 let errCode: number = err.code; 447 if (errCode) { 448 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 449 return; 450 } 451 console.info('Succeeded in loading the content.'); 452 }); 453 } 454}; 455``` 456 457<!--RP2--> 458## 设置悬浮窗<!--RP2End--> 459 460悬浮窗可以在已有的任务基础上,创建一个始终在前台显示的窗口。即使创建悬浮窗的任务退至后台,悬浮窗仍然可以在前台显示。通常悬浮窗位于所有应用窗口之上,开发者可以创建悬浮窗,并对悬浮窗进行属性设置等操作。 461 462 463### 开发步骤 464 465<!--RP1--> 466**前提条件:** 创建`WindowType.TYPE_FLOAT`即悬浮窗类型的窗口,需要申请`ohos.permission.SYSTEM_FLOAT_WINDOW`权限,配置方式请参见[system_basic等级应用申请权限的方式](../security/AccessToken/determine-application-mode.md#system_basic等级应用申请权限的方式)。 467<!--RP1End--> 468 4691. 创建悬浮窗。 470 471 通过`window.createWindow`接口创建悬浮窗类型的窗口。 472 4732. 对悬浮窗进行属性设置等操作。 474 475 悬浮窗窗口创建成功后,可以改变其大小、位置等,还可以根据应用需要设置悬浮窗背景色、亮度等属性。 476 4773. 加载显示悬浮窗的具体内容。 478 479 通过`setUIContent`和`showWindow`接口加载显示悬浮窗的具体内容。 480 4814. 销毁悬浮窗。 482 483 当不再需要悬浮窗时,可根据具体实现逻辑,使用`destroyWindow`接口销毁悬浮窗。 484 485```ts 486import { UIAbility } from '@kit.AbilityKit'; 487import { window } from '@kit.ArkUI'; 488import { BusinessError } from '@kit.BasicServicesKit'; 489 490export default class EntryAbility extends UIAbility { 491 onWindowStageCreate(windowStage: window.WindowStage) { 492 // 1.创建悬浮窗。 493 let windowClass: window.Window | null = null; 494 let config: window.Configuration = { 495 name: "floatWindow", windowType: window.WindowType.TYPE_FLOAT, ctx: this.context 496 }; 497 window.createWindow(config, (err: BusinessError, data) => { 498 let errCode: number = err.code; 499 if (errCode) { 500 console.error('Failed to create the floatWindow. Cause: ' + JSON.stringify(err)); 501 return; 502 } 503 console.info('Succeeded in creating the floatWindow. Data: ' + JSON.stringify(data)); 504 windowClass = data; 505 // 2.悬浮窗窗口创建成功后,设置悬浮窗的位置、大小及相关属性等。 506 windowClass.moveWindowTo(300, 300, (err: BusinessError) => { 507 let errCode: number = err.code; 508 if (errCode) { 509 console.error('Failed to move the window. Cause:' + JSON.stringify(err)); 510 return; 511 } 512 console.info('Succeeded in moving the window.'); 513 }); 514 windowClass.resize(500, 500, (err: BusinessError) => { 515 let errCode: number = err.code; 516 if (errCode) { 517 console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); 518 return; 519 } 520 console.info('Succeeded in changing the window size.'); 521 }); 522 // 3.为悬浮窗加载对应的目标页面。 523 windowClass.setUIContent("pages/page4", (err: BusinessError) => { 524 let errCode: number = err.code; 525 if (errCode) { 526 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 527 return; 528 } 529 console.info('Succeeded in loading the content.'); 530 // 3.显示悬浮窗。 531 (windowClass as window.Window).showWindow((err: BusinessError) => { 532 let errCode: number = err.code; 533 if (errCode) { 534 console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); 535 return; 536 } 537 console.info('Succeeded in showing the window.'); 538 }); 539 }); 540 // 4.销毁悬浮窗。当不再需要悬浮窗时,可根据具体实现逻辑,使用destroy对其进行销毁。 541 windowClass.destroyWindow((err: BusinessError) => { 542 let errCode: number = err.code; 543 if (errCode) { 544 console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); 545 return; 546 } 547 console.info('Succeeded in destroying the window.'); 548 }); 549 }); 550 } 551}; 552``` 553 554## 监听窗口不可交互与可交互事件 555 556应用在前台显示过程中可能会进入某些不可交互的场景,比较典型的是进入多任务界面。此时,对于一些应用可能需要选择暂停某个与用户正在交互的业务,如视频类应用暂停正在播放的视频或者相机暂停预览流等。而当该应用从多任务又切回前台时,又变成了可交互的状态,此时需要恢复被暂停中断的业务,如恢复视频播放或相机预览流等。 557 558### 开发步骤 559 560在创建WindowStage对象后可通过监听`'windowStageEvent'`事件类型,监听到窗口进入前台、后台、前台可交互、前台不可交互等事件,应用可根据这些上报的事件状态进行相应的业务处理。 561 562```ts 563import { UIAbility } from '@kit.AbilityKit'; 564import { window } from '@kit.ArkUI'; 565 566export default class EntryAbility extends UIAbility { 567 onWindowStageCreate(windowStage: window.WindowStage) { 568 try { 569 windowStage.on('windowStageEvent', (data) => { 570 console.info('Succeeded in enabling the listener for window stage event changes. Data: ' + 571 JSON.stringify(data)); 572 573 // 根据事件状态类型选择进行相应的处理 574 if (data == window.WindowStageEventType.SHOWN) { 575 console.info('current window stage event is SHOWN'); 576 // 应用进入前台,默认为可交互状态 577 // ... 578 } else if (data == window.WindowStageEventType.HIDDEN) { 579 console.info('current window stage event is HIDDEN'); 580 // 应用进入后台,默认为不可交互状态 581 // ... 582 } else if (data == window.WindowStageEventType.PAUSED) { 583 console.info('current window stage event is PAUSED'); 584 // 前台应用进入多任务,转为不可交互状态 585 // ... 586 } else if (data == window.WindowStageEventType.RESUMED) { 587 console.info('current window stage event is RESUMED'); 588 // 进入多任务后又继续返回前台时,恢复可交互状态 589 // ... 590 } 591 592 // ... 593 }); 594 } catch (exception) { 595 console.error('Failed to enable the listener for window stage event changes. Cause:' + 596 JSON.stringify(exception)); 597 } 598 } 599} 600``` 601 602## 相关实例 603 604针对window开发(Stage模型),有以下相关实例可供参考: 605 606- [`Window`:一多设置典型页面(Settings)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-5.0.1-Release/code/SuperFeature/MultiDeviceAppDev/Settings) 607 608- [悬浮窗(ArkTS)(API10)(Full SDK)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-5.0.1-Release/code/SystemFeature/WindowManagement/WindowRatio) 609 610- [窗口管理(ArkTS)(API12)(Full SDK)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-5.0.1-Release/code/SystemFeature/WindowManagement/WindowManage)