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