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