文档管理中心

@ohos.window.floatView (闪控窗)

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗是悬浮在桌面/应用界面上的小型窗口,提供灵活的窗口管理能力。

本模块提供闪控窗能力,包括判断设备是否支持闪控窗功能、创建闪控窗控制器以启动、更新或停止闪控窗等。

适用场景:

闪控窗适用于需要在独立小窗口中持续展示应用内容或提供快捷操作的场景。例如:

  • 股市盯盘应用:用户在浏览其他应用时,通过闪控窗实时查看股票行情变化,无需频繁切换应用。
  • 手机直播应用:主播在直播过程中使用闪控窗展示自定义的互动面板或控制界面,方便实时操作和互动。

闪控窗和闪控球对比

  • 共同点:闪控窗和闪控球均为一种特殊的应用辅助窗口,具备在应用主窗口和对应UIAbility(应用组件)退至后台后仍然可以在前台显示的能力。可以用于应用退至后台后,使用闪控窗或闪控球继续显示UI。
  • 区别:
    • 显示形式不同。闪控球以小圆球的形式展现,适用于展示关键信息。闪控窗以小型窗口展示,展示区域较大,可以持续展示应用内容或提供快捷操作。
    • 闪控球只能贴边展示,闪控窗则没有此限制。
    • 闪控球模板固定,应用不能定制UI。闪控窗同样存在模板,并由系统管理并统一绘制UI,但是提供了可绘制的区域,可供应用加载指定页面内容。

与闪控球联动:

本模块可与@ohos.window.floatingBall(闪控球)联合使用。通过floatView.bind接口将闪控窗控制器与闪控球控制器绑定后,用户点击闪控球可展开为闪控窗,点击闪控窗左上角的缩小按钮可收起为闪控球,实现两种窗口形态的相互切换。

全局悬浮窗和闪控窗对比

  • 共同点:全局悬浮窗和闪控窗均为一种特殊的应用辅助窗口,具备在应用主窗口和对应UIAbility退至后台后仍然可以在前台显示的能力。可以用于应用退至后台后,使用全局悬浮窗或闪控窗继续显示UI。
  • 区别:
    • 全局悬浮窗由开发者管理并实现UI绘制,无统一UI及动效。
    • 闪控窗由系统管理并统一绘制UI,动效更为高端精致。
    • 闪控窗支持与闪控球互相绑定联合使用,实现更复杂场景。

起始版本: 26.0.0

说明
  • 针对系统能力SystemCapability.Window.SessionManager,请先使用canIUse()接口判断当前设备是否支持此syscap及对应接口。

  • 本模块接口仅可在Stage模型下使用。

导入模块

收起
自动换行
深色代码主题
复制
  1. import { floatView } from '@kit.ArkUI';

floatView.isFloatViewEnabled

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

isFloatViewEnabled(): boolean

判断当前设备是否支持闪控窗功能。

起始版本: 26.0.0

系统能力: SystemCapability.Window.SessionManager

模型约束: 此接口仅可在Stage模型下使用。

返回值:

展开
类型 说明
boolean 当前设备是否支持闪控窗功能。true表示支持,false则表示不支持。

示例:

收起
自动换行
深色代码主题
复制
  1. // 判断当前设备是否支持闪控窗功能
  2. let enable: boolean = floatView.isFloatViewEnabled();
  3. console.info('Float view enabled is: ' + enable);

floatView.create

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

create(config: FloatViewConfiguration): Promise<FloatViewController>

创建闪控窗控制器。使用Promise异步回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
config FloatViewConfiguration 创建闪控窗控制器的参数。该参数及其context字段不能为null或undefined,否则抛出401。其他参数异常情况抛出1300016。

返回值:

展开
类型 说明
Promise<FloatViewController> Promise对象。返回当前创建的闪控窗控制器。

错误码:

以下错误码的详细介绍请参见通用错误码窗口错误码

展开
错误码ID 错误信息
801 Capability not supported. Possible cause: Call the API on unsupported device.
1300002 This window state is abnormal. Possible cause: 1. This window context is abnormal. 2. System error, such as a null pointer, insufficient memory or a JS engine exception.
1300016 Parameter error. Possible cause: Invalid template type.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { common } from '@kit.AbilityKit';
  3. import { floatView } from '@kit.ArkUI';
  4. @Entry
  5. @Component
  6. struct Index {
  7. private floatViewController: floatView.FloatViewController | undefined = undefined;
  8. aboutToAppear(): void {
  9. // 请在组件内获取context,确保this.getUIContext().getHostContext()返回的结果为UIAbilityContext
  10. let ctx = this.getUIContext().getHostContext() as common.UIAbilityContext;
  11. // 创建闪控窗配置对象
  12. let config: floatView.FloatViewConfiguration = {
  13. context: ctx,
  14. templateType: floatView.FloatViewTemplateType.ROUNDED_RECTANGLE
  15. };
  16. try {
  17. // 创建闪控窗控制器
  18. floatView.create(config).then((data: floatView.FloatViewController) => {
  19. this.floatViewController = data;
  20. console.info(`Succeeded in creating float view controller. Data: ${data}`);
  21. }).catch((err: BusinessError): void => {
  22. console.error(`Failed to create float view controller. Cause:${err.code}, message:${err.message}`);
  23. });
  24. } catch (e) {
  25. console.error(`Failed to create float view controller. Cause:${e.code}, message:${e.message}`);
  26. }
  27. }
  28. }

floatView.bind

Phone26.0.0+Tablet26.0.0+

bind(floatViewController: FloatViewController, floatingBallController: floatingBall.FloatingBallController, floatingBallParams: floatingBall.FloatingBallParams): Promise<void>

绑定闪控窗和闪控球。需要先创建闪控窗控制器闪控球控制器,且均未启动。使用Promise异步回调。

说明
  • 绑定成功后,调用start()startFloatingBall()均会同时创建闪控窗窗口和闪控球窗口,并触发对应窗口已注册的状态回调。但同一时刻仅展示其中一个窗口,展示顺序取决于先调用哪个控制器的启动接口。
  • 绑定成功后,用户可通过点击操作在闪控窗窗口与闪控球之间进行切换。
  • 绑定成功后,调用任一控制器的停止接口(stop()stopFloatingBall())会同时销毁闪控窗窗口和闪控球窗口,并触发对应窗口已注册的状态回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.USE_FLOAT_BALL 和 ohos.permission.FLOAT_VIEW

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
floatViewController FloatViewController 闪控窗控制器,用于管理闪控窗的启动、停止和状态监听等操作。
floatingBallController floatingBall.FloatingBallController 闪控球控制器,用于管理闪控球的启动、停止和状态监听等操作。
floatingBallParams floatingBall.FloatingBallParams 闪控球参数。绑定时设置的参数会覆盖掉闪控球控制器启动时已保存的参数。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见通用错误码窗口错误码

展开
错误码ID 错误信息
201 Permission verification failed. Possible cause: The application does not have the permission required to call the API.
801 Capability not supported on this device. Possible cause: Call api on unsupported device.
1300019 Wrong parameters for operating the floating ball. Possible cause: Invalid floating ball params.
1300025 The floating ball state does not support this operation. Possible cause: 1. The floating ball has started but not stopped yet. 2. The floating ball controller has been bound.
1300031 The floatView state does not support this operation. Possible cause: 1. The float view has started but not stopped yet. 2. The float view controller has been bound.

示例:

收起
自动换行
深色代码主题
复制
  1. // Entry.ets
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { floatingBall, floatView } from '@kit.ArkUI';
  4. @Entry
  5. @Component
  6. struct Index {
  7. private floatingBallController: floatingBall.FloatingBallController | undefined = undefined;
  8. private floatViewController: floatView.FloatViewController | undefined = undefined;
  9. // 创建控制器
  10. // ...
  11. public bindController(): void {
  12. let floatingBallParams: floatingBall.FloatingBallParams = {
  13. template: floatingBall.FloatingBallTemplate.EMPHATIC,
  14. title: 'title',
  15. content: 'content'
  16. };
  17. try {
  18. if (this.floatViewController && this.floatingBallController) {
  19. // 绑定闪控窗和闪控球
  20. floatView.bind(this.floatViewController!, this.floatingBallController!, floatingBallParams).then(() => {
  21. console.info('Succeeded in binding float view and floating ball.');
  22. }).catch((err: BusinessError): void => {
  23. console.error(`Failed to bind float view and floating ball. Cause:${err.code}, message:${err.message}`);
  24. });
  25. }
  26. } catch (e) {
  27. console.error(`Failed to bind float view and floating ball. Cause:${e.code}, message:${e.message}`);
  28. }
  29. }
  30. }

floatView.unbind

Phone26.0.0+Tablet26.0.0+

unbind(floatViewController: FloatViewController, floatingBallController: floatingBall.FloatingBallController): Promise<void>

解绑闪控窗和闪控球。需要在闪控窗控制器闪控球控制器均停止后才可解绑。使用Promise异步回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
floatViewController FloatViewController 闪控窗控制器,用于管理闪控窗的启动、停止和状态监听等操作。
floatingBallController floatingBall.FloatingBallController 闪控球控制器,用于管理闪控球的启动、停止和状态监听等操作。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见通用错误码窗口错误码

展开
错误码ID 错误信息
801 Capability not supported on this device. Possible cause: Call api on unsupported device.
1300025 The floating ball state does not support this operation. Possible cause: 1. The floating ball has started but not stopped yet. 2. The floatingBallController has not been bound.
1300031 The floatView state does not support this operation. Possible cause: 1. The float view has started but not stopped yet. 2. The floatViewController has not been bound. 3. The floatViewController and the floatingBallController are not bound together.

示例:

收起
自动换行
深色代码主题
复制
  1. // Entry.ets
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { floatingBall, floatView } from '@kit.ArkUI';
  4. @Entry
  5. @Component
  6. struct Index {
  7. private floatingBallController: floatingBall.FloatingBallController | undefined = undefined;
  8. private floatViewController: floatView.FloatViewController | undefined = undefined;
  9. // 创建控制器
  10. // ...
  11. public unbindController(): void {
  12. try {
  13. // 使用绑定时传入的闪控窗和闪控球控制器
  14. if (this.floatViewController && this.floatingBallController) {
  15. // 解绑闪控窗和闪控球
  16. floatView.unbind(this.floatViewController!, this.floatingBallController!).then(() => {
  17. console.info('Succeeded in unbinding float view and floating ball.');
  18. }).catch((err: BusinessError): void => {
  19. console.error(`Failed to unbind float view and floating ball. Cause:${err.code}, message:${err.message}`);
  20. });
  21. }
  22. } catch (e) {
  23. console.error(`Failed to unbind float view and floating ball. Cause:${e.code}, message:${e.message}`);
  24. }
  25. }
  26. }

floatView.getFloatViewLimits

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

getFloatViewLimits(templateType: FloatViewTemplateType): FloatViewLimits

根据传入的模板类型获取对应闪控窗窗口的限制,单位为px。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
templateType FloatViewTemplateType 闪控窗模板类型。

返回值:

展开
类型 说明
FloatViewLimits 返回闪控窗窗口的限制,包括最大尺寸、最小尺寸和宽高比的限制范围。

错误码:

以下错误码的详细介绍请参见通用错误码窗口错误码

展开
错误码ID 错误信息
801 Capability not supported. Possible cause: Call the API on unsupported device.
1300002 This window state is abnormal. Possible cause: System error, such as a null pointer, insufficient memory or a JS engine exception.
1300003 This window manager service works abnormally. Possible cause: Internal IPC error.
1300016 Parameter error. Possible cause: Invalid template type.

示例:

收起
自动换行
深色代码主题
复制
  1. // 获取圆角矩形模板的闪控窗窗口限制
  2. let limits: floatView.FloatViewLimits = floatView.getFloatViewLimits(floatView.FloatViewTemplateType.ROUNDED_RECTANGLE);
  3. console.info('Float view limits: ' + JSON.stringify(limits));

FloatViewConfiguration

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

创建闪控窗控制器时需要提供的参数配置。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 类型 只读 可选 说明
context BaseContext 表示上下文环境,用于创建闪控窗控制器时关联应用主窗口。必须传入有效的UIAbilityContext实例。
templateType FloatViewTemplateType 闪控窗的模板类型。
isConfirmOnClose boolean 点击关闭按钮时是否需要用户确认。true表示点击关闭按钮时需要用户确认,否则不需要确认。默认值为false。

TemplateProperty

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

切换闪控窗模板并修改窗口尺寸时需要提供的参数配置。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 类型 只读 可选 说明
templateType FloatViewTemplateType 闪控窗的模板类型。
size window.Size 更新模板类型时需要提供的窗口尺寸。

FloatViewController

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗控制器实例。用于启动、停止闪控窗以及注册回调等操作。

下列API示例中都需先使用floatView.create()方法获取到闪控窗控制器实例(即floatViewController),再通过此实例调用对应方法。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

setUIContext

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

setUIContext(path: string, storage?: LocalStorage): Promise<void>

根据当前工程中指定的页面路径为闪控窗加载具体页面内容,通过LocalStorage传递状态属性至加载页面。使用Promise异步回调。

建议在闪控窗启动前使用该接口,重复调用将先销毁旧的页面内容(即UIContent)再加载新的页面内容,请谨慎使用。

本接口不支持加载跨包页面,若有需要请使用setUIContextByName()接口。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
path string 要加载到窗口中的页面内容的路径,该路径需添加到工程的main_pages.json文件中。不支持相对路径写法,需与main_pages.json中的src取值保持一致。若路径无效或不满足上述要求,将抛出错误码1300016。
storage LocalStorage 页面级UI状态存储单元,用于为加载到窗口的页面内容传递状态属性。默认值为空。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300016 Parameter error. Possible causes: Invalid path.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { floatView } from '@kit.ArkUI';
  3. try {
  4. // floatViewController需通过floatView.create()获取,详见floatView.create()示例
  5. // 设置闪控窗的页面内容路径
  6. this.floatViewController?.setUIContext('pages/Index').then(() => {
  7. console.info('Succeeded in setting UI context.');
  8. }).catch((err: BusinessError): void => {
  9. console.error(`Failed to set UI context. Cause:${err.code}, message:${err.message}`);
  10. });
  11. } catch (e) {
  12. console.error(`Failed to set UI context. Cause:${e.code}, message:${e.message}`);
  13. }

setUIContextByName

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

setUIContextByName(name: string, storage?: LocalStorage): Promise<void>

根据指定路由页面名称为当前窗口加载命名路由页面,通过LocalStorage传递状态属性至加载页面,使用Promise异步回调。

建议在闪控窗启动前使用该接口,重复调用将先销毁旧的页面内容(即UIContent)再加载新的页面内容,请谨慎使用。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
name string 命名路由页面的名称,用于加载指定的命名路由页面内容。需与@Entry装饰器中routeName参数指定的名称一致。
storage LocalStorage 页面级UI状态存储单元,用于为加载到窗口的页面内容传递状态属性。默认值为空。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300016 Parameter error. Possible causes: Invalid name.

示例:

收起
自动换行
深色代码主题
复制
  1. // Index.ets
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { entryName } from './Hello'; // 导入命名路由页面
  4. import { floatView } from '@kit.ArkUI';
  5. @Entry
  6. @Component
  7. struct Index {
  8. private floatViewController: floatView.FloatViewController | undefined = undefined;
  9. // 创建控制器
  10. // ...
  11. public setUIContextByName(): void {
  12. try {
  13. // 根据命名路由名称设置闪控窗的页面内容
  14. this.floatViewController?.setUIContextByName(entryName).then(() => {
  15. console.info('Succeeded in loading the content.');
  16. }).catch((err: BusinessError): void => {
  17. console.error(`Failed to load the content. Cause code: ${err.code}, message: ${err.message}`);
  18. });
  19. } catch (e) {
  20. console.error(`Failed to load the content. Cause code: ${e.code}, message: ${e.message}`);
  21. }
  22. }
  23. }
收起
自动换行
深色代码主题
复制
  1. // Hello.ets
  2. export const entryName : string = 'Hello';
  3. @Entry({routeName: entryName, useSharedStorage: true})
  4. @Component
  5. export struct Hello {
  6. @State message: string = 'Hello World'
  7. build() {
  8. Row() {
  9. Column() {
  10. Text(this.message)
  11. }
  12. .width('100%')
  13. }
  14. .height('100%')
  15. }
  16. }

setWindowSize

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

setWindowSize(size: window.Size): Promise<void>

设置闪控窗窗口大小。建议先调用getFloatViewLimits接口获取推荐的宽高范围和宽高比范围,再根据推荐值调用本接口。窗口实际大小变化可通过onRectChange接口监听。使用Promise异步回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
size window.Size 表示窗口的大小,单位为px,宽度和高度必须大于0,超出有效范围时抛出错误码1300016。建议大小满足getFloatViewLimits接口返回的限制。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300003 This window manager service works abnormally. Possible cause: Internal IPC error.
1300016 Parameter error. Possible cause: The value of the size is less than or equal to 0.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { floatView, window } from '@kit.ArkUI';
  3. // 设置窗口大小
  4. let size: window.Size = {
  5. width: 400,
  6. height: 600
  7. };
  8. try {
  9. // 设置闪控窗窗口大小
  10. this.floatViewController?.setWindowSize(size).then(() => {
  11. console.info('Succeeded in setting window size.');
  12. }).catch((err: BusinessError): void => {
  13. console.error(`Failed to set window size. Cause:${err.code}, message:${err.message}`);
  14. });
  15. } catch (e) {
  16. console.error(`Failed to set window size. Cause:${e.code}, message:${e.message}`);
  17. }

switchTemplate

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

switchTemplate(templateProperty: TemplateProperty): Promise<void>

切换闪控窗的模板并改变其窗口尺寸。建议先调用getFloatViewLimits接口获取目标模板类型推荐的宽高范围和宽高比范围,再根据推荐值调用本接口。窗口实际大小变化可通过onRectChange接口监听。使用Promise异步回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
templateProperty TemplateProperty 表示需要切换的窗口模板类型及大小。size中的宽度和高度必须大于0,超出有效范围时抛出错误码1300016。建议大小满足getFloatViewLimits接口返回的限制。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300003 This window manager service works abnormally. Possible cause: Internal IPC error.
1300016 Parameter error. Possible cause: 1. Invalid template type. 2. The value of the size is less than or equal to 0.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { floatView, window } from '@kit.ArkUI';
  3. // 设置新窗口大小
  4. let newSize: window.Size = {
  5. width: 800,
  6. height: 100
  7. };
  8. // 设置模板属性
  9. let templateProperty: floatView.TemplateProperty = {
  10. templateType: floatView.FloatViewTemplateType.HORIZONTAL_BAR,
  11. size: newSize
  12. };
  13. try {
  14. // 切换闪控窗模板并改变窗口尺寸
  15. this.floatViewController?.switchTemplate(templateProperty).then(() => {
  16. console.info('Succeeded in switching window type and size.');
  17. }).catch((err: BusinessError): void => {
  18. console.error(`Failed to switch window type and size. Cause:${err.code}, message:${err.message}`);
  19. });
  20. } catch (e) {
  21. console.error(`Failed to switch window type and size. Cause:${e.code}, message:${e.message}`);
  22. }

start

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

start(): Promise<void>

启动闪控窗窗口。接口返回不表示start流程结束,需要通过onStateChange接口监听到STARTED回调时判断启动成功。建议在调用setUIContext()setUIContextByName()后调用start()。使用Promise异步回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.FLOAT_VIEW

系统能力: SystemCapability.Window.SessionManager

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见通用错误码窗口错误码

展开
错误码ID 错误信息
201 Permission verification failed. Possible cause: The application does not have the permission required to call the API.
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300003 This window manager service works abnormally. Possible cause: Internal IPC error.
1300030 Repeated operations on the float view. Possible cause: The float view is starting or has already started.
1300031 The float view state does not support this operation. Possible cause: The float view is stopping.
1300033 Failed to start float view. Possible causes: 1. Start multiple float views. 2. The main window of context is not foreground.
1300034 This operation conflicts with other floating windows. Possible cause: App has already started floating ball or pip window.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { floatView } from '@kit.ArkUI';
  3. try {
  4. // 启动闪控窗
  5. this.floatViewController?.start().then(() => {
  6. console.info('Succeeded in starting float view.');
  7. }).catch((err: BusinessError): void => {
  8. console.error(`Failed to start float view. Cause:${err.code}, message:${err.message}`);
  9. });
  10. } catch (e) {
  11. console.error(`Failed to start float view. Cause:${e.code}, message:${e.message}`);
  12. }

stop

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

stop(): Promise<void>

停止闪控窗窗口。接口返回不表示stop流程结束,需要通过onStateChange接口监听到STOPPED回调时判断停止成功。使用Promise异步回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300003 This window manager service works abnormally. Possible cause: Internal IPC error.
1300030 Repeated operations on the float view. Possible cause: The float view is stopping or has already stopped.
1300031 This operation is not supported on the float view in the current state. Possible cause: The float view window is not started.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { floatView } from '@kit.ArkUI';
  3. try {
  4. // 停止闪控窗
  5. this.floatViewController?.stop().then(() => {
  6. console.info('Succeeded in stopping float view.');
  7. }).catch((err: BusinessError): void => {
  8. console.error(`Failed to stop float view. Cause:${err.code}, message:${err.message}`);
  9. });
  10. } catch (e) {
  11. console.error(`Failed to stop float view. Cause:${e.code}, message:${e.message}`);
  12. }

setFloatViewVisibilityInApp

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

setFloatViewVisibilityInApp(isVisible: boolean): Promise<void>

设置应用在前台时闪控窗窗口是否可见。使用Promise异步回调。

创建闪控窗后未调用此接口前,默认其在应用处于前台时为可见状态。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
isVisible boolean 应用在前台时闪控窗是否可见,true表示可见,false表示不可见。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300003 This window manager service works abnormally. Possible cause: Internal IPC error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { floatView } from '@kit.ArkUI';
  3. try {
  4. // 设置应用在前台时闪控窗可见
  5. this.floatViewController?.setFloatViewVisibilityInApp(true).then(() => {
  6. console.info('Succeeded in setting float view visibility in app.');
  7. }).catch((err: BusinessError): void => {
  8. console.error(`Failed to set float view visibility in app. Cause:${err.code}, message:${err.message}`);
  9. });
  10. } catch (e) {
  11. console.error(`Failed to set float view visibility in app. Cause:${e.code}, message:${e.message}`);
  12. }

restoreMainWindow

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

restoreMainWindow(wantParameters?: Record<string, Object>): Promise<void>

恢复闪控窗的主窗口到前台显示。如果主窗口已处于前台时调用,将抬升主窗口层级。此接口要求闪控窗处于STARTED状态,且只能在用户点击闪控窗窗口后调用。当主窗口处于PAUSED生命周期或处于多任务状态时,调用接口将抛出错误码1300032。使用Promise异步回调。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
wantParameters Record<string, Object> 恢复闪控窗的主窗口时会给主窗口传递的自定义参数,主窗口会在触发onNewWant回调时收到。默认值为空,代表不向主窗口传入任何自定义参数。

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300003 This window manager service works abnormally. Possible cause: Internal IPC error.
1300031 This operation is not supported on the float view in the current state. Possible cause: The float view window is not started when restoring.
1300032 Failed to restore the main window. Possible cause: 1. User has never clicked the float view window before restore. 2. The float view window is not in the foreground. 3. The main window is in PAUSED lifecycle state. 4. The main window is in background during recent.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from '@kit.BasicServicesKit';
  2. import { floatView } from '@kit.ArkUI';
  3. // 创建恢复主窗口的参数
  4. let param: Record<string, Object> = {
  5. 'info': 'helloworld',
  6. };
  7. // 闪控窗状态需是STARTED
  8. try {
  9. // 恢复闪控窗的主窗口到前台显示
  10. this.floatViewController?.restoreMainWindow(param).then(() => {
  11. console.info('Succeeded in restoring main window.');
  12. }).catch((err: BusinessError): void => {
  13. console.error(`Failed to restore main window. Cause:${err.code}, message:${err.message}`);
  14. });
  15. } catch (e) {
  16. console.error(`Failed to restore main window. Cause:${e.code}, message:${e.message}`);
  17. }

getWindowProperties

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

getWindowProperties(): FloatViewProperties

获取闪控窗窗口的属性。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

返回值:

展开
类型 说明
FloatViewProperties 返回闪控窗窗口的属性。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300031 This operation is not supported on the float view in the current state. Possible cause: The float view window has not started, has stopped, or is in an error state.

示例:

收起
自动换行
深色代码主题
复制
  1. try {
  2. // 获取闪控窗窗口属性
  3. let properties: floatView.FloatViewProperties | undefined = this.floatViewController?.getWindowProperties();
  4. console.info('Float view properties: ' + JSON.stringify(properties));
  5. } catch (e) {
  6. console.error(`Failed to get window properties. Cause:${e.code}, message:${e.message}`);
  7. }

onStateChange

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

onStateChange(callback: Callback<FloatViewStateChangeInfo>): void

注册闪控窗状态变化的监听事件。不再使用时,取消监听以避免内存泄漏。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
callback Callback<FloatViewStateChangeInfo> 回调函数。返回当前的闪控窗状态变化信息。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300030 Repeated operations on the float view. Possible cause: The callback has already registered.

示例:

收起
自动换行
深色代码主题
复制
  1. // 定义状态变化回调函数
  2. let onStateChange = (info: floatView.FloatViewStateChangeInfo) => {
  3. console.info('Float view stateChange: ' + JSON.stringify(info));
  4. };
  5. try {
  6. // 注册闪控窗状态变化监听
  7. this.floatViewController?.onStateChange(onStateChange);
  8. } catch (e) {
  9. console.error(`Failed to on stateChange float view. Cause:${e.code}, message:${e.message}`);
  10. }

offStateChange

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

offStateChange(callback?: Callback<FloatViewStateChangeInfo>): void

取消闪控窗状态变化的监听事件。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
callback Callback<FloatViewStateChangeInfo> 回调函数。返回当前的闪控窗状态变化信息。若传入参数,则停止该监听。若未传入参数,则停止所有闪控窗状态变化的监听。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.

示例:

收起
自动换行
深色代码主题
复制
  1. // 定义状态变化回调函数
  2. let onStateChange = (info: floatView.FloatViewStateChangeInfo) => {
  3. console.info('Float view stateChange: ' + JSON.stringify(info));
  4. };
  5. try {
  6. // 取消闪控窗状态变化监听
  7. this.floatViewController?.offStateChange(onStateChange);
  8. } catch (e) {
  9. console.error(`Failed to off stateChange float view. Cause:${e.code}, message:${e.message}`);
  10. }

onRectChange

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

onRectChange(callback: Callback<FloatViewRectChangeInfo>): void

注册闪控窗矩形区域(位置和大小)变化的监听事件。不再使用时,取消监听以避免内存泄漏。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
callback Callback<FloatViewRectChangeInfo> 回调函数。返回当前的闪控窗矩形区域变化信息。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300030 Repeated operations on the float view. Possible cause: The callback has already registered.

示例:

收起
自动换行
深色代码主题
复制
  1. // 定义矩形区域变化回调函数
  2. let onRectChange = (info: floatView.FloatViewRectChangeInfo) => {
  3. console.info('Float view rectChange: ' + JSON.stringify(info));
  4. };
  5. try {
  6. // 注册闪控窗矩形区域变化监听
  7. this.floatViewController?.onRectChange(onRectChange);
  8. } catch (e) {
  9. console.error(`Failed to on rectChange float view. Cause:${e.code}, message:${e.message}`);
  10. }

offRectChange

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

offRectChange(callback?: Callback<FloatViewRectChangeInfo>): void

取消闪控窗矩形区域变化的监听事件。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
callback Callback<FloatViewRectChangeInfo> 回调函数。返回当前的闪控窗矩形区域变化信息。若传入参数,则停止该监听。若未传入参数,则停止所有闪控窗矩形区域变化的监听。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.

示例:

收起
自动换行
深色代码主题
复制
  1. // 定义矩形区域变化回调函数
  2. let onRectChange = (info: floatView.FloatViewRectChangeInfo) => {
  3. console.info('Float view rectChange: ' + JSON.stringify(info));
  4. };
  5. try {
  6. // 取消闪控窗矩形区域变化监听
  7. this.floatViewController?.offRectChange(onRectChange);
  8. } catch (e) {
  9. console.error(`Failed to off rectChange float view. Cause:${e.code}, message:${e.message}`);
  10. }

onLimitsChange

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

onLimitsChange(callback: Callback<FloatViewLimits>): void

注册闪控窗限制变化的监听事件。当限制规格变化时(例如折叠展开导致屏幕宽度变化或切换模板),触发回调并返回当前窗口模板类型的限制信息。不再使用时,取消监听以避免内存泄漏。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
callback Callback<FloatViewLimits> 回调函数。返回当前的闪控窗限制变化信息。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.
1300030 Repeated operations on the float view. Possible cause: The callback has already registered.

示例:

收起
自动换行
深色代码主题
复制
  1. // 定义限制变化回调函数
  2. let onLimitsChange = (limits: floatView.FloatViewLimits) => {
  3. console.info('Float view limitsChange: ' + JSON.stringify(limits));
  4. };
  5. try {
  6. // 注册闪控窗限制变化监听
  7. this.floatViewController?.onLimitsChange(onLimitsChange);
  8. } catch (e) {
  9. console.error(`Failed to on limitsChange float view. Cause:${e.code}, message:${e.message}`);
  10. }

offLimitsChange

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+

offLimitsChange(callback?: Callback<FloatViewLimits>): void

取消闪控窗限制变化的监听事件。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

参数:

展开
参数名 类型 必填 说明
callback Callback<FloatViewLimits> 回调函数。返回当前的闪控窗限制变化信息。若传入参数,则停止该监听。若未传入参数,则停止所有闪控窗限制变化的监听。

错误码:

以下错误码的详细介绍请参见窗口错误码

展开
错误码ID 错误信息
1300002 This window state is abnormal. Possible cause: The float view controller object is null.

示例:

收起
自动换行
深色代码主题
复制
  1. // 定义限制变化回调函数
  2. let onLimitsChange = (limits: floatView.FloatViewLimits) => {
  3. console.info('Float view limitsChange: ' + JSON.stringify(limits));
  4. };
  5. try {
  6. // 取消闪控窗限制变化监听
  7. this.floatViewController?.offLimitsChange(onLimitsChange);
  8. } catch (e) {
  9. console.error(`Failed to off limitsChange float view. Cause:${e.code}, message:${e.message}`);
  10. }

FloatViewTemplateType

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗模板类型的枚举。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 说明
ROUNDED_RECTANGLE 0 圆角矩形。
HORIZONTAL_BAR 1 水平的条状矩形。

FloatViewProperties

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗窗口的属性。

起始版本: 26.0.0

系统能力: SystemCapability.Window.SessionManager

模型约束: 此接口仅可在Stage模型下使用。

展开
名称 类型 只读 可选 说明
templateType FloatViewTemplateType 闪控窗的模板类型。
windowId number 闪控窗窗口ID。
displayId number 闪控窗所在屏幕ID。
windowRect window.Rect 闪控窗窗口矩形区域。
windowScale number 闪控窗窗口缩放比例。
avoidArea window.AvoidArea

闪控窗内容的避让区域。

注意:

通过setUIContext()setUIContextByName()加载的页面中,位于避让区域的组件将不响应手势事件,开发者在添加需要手势响应事件的组件时,请注意避让这些区域。

inSidebar boolean 闪控窗是否在侧边栏中。true为在侧边栏中,false为不在侧边栏中。

RatioLimit

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗的宽高比限制范围。宽高比比值由窗口矩形区域的宽除以高获得。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 类型 只读 可选 说明
minRatio number 闪控窗的宽高比最小值。
maxRatio number 闪控窗的宽高比最大值。

FloatViewLimits

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗窗口的限制。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 类型 只读 可选 说明
minSize window.Size 闪控窗的最小尺寸。
maxSize window.Size 闪控窗的最大尺寸。
ratioLimits Array<RatioLimit> 闪控窗的宽高比限制范围,数组中每个元素包含minRatio(最小宽高比)和maxRatio(最大宽高比)。

FloatViewStateChangeInfo

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗状态变化信息。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 类型 只读 可选 说明
state FloatViewState 闪控窗的状态。
stopReason string

闪控窗停止的原因。该参数仅在状态为FloatViewState.STOPPED时有效,在其他状态下默认为空字符串。停止原因和对应含义如下:

"APP_STOP":应用主动停止

"STOP_IN_SIDEBAR":在侧边栏被关闭

"TITLE_BAR_STOP_CLICK":标题栏点击关闭按钮

"DUMPSTER_STOP":拖入垃圾桶停止

"REPLACE_STOP":被其他闪控窗挤占

"FLOATING_BALL_STOP":绑定状态下跟随闪控球停止

"MAIN_WINDOW_DESTROY_STOP":context关联的主窗被销毁后停止

FloatViewState

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗状态的枚举。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 说明
STARTED 1 闪控窗已启动并显示。
HIDDEN 2 闪控窗已隐藏。上滑进入多任务界面时触发;或使用setFloatViewVisibilityInApp接口设置应用在前台时隐藏闪控窗后,应用处于前台时触发。
STOPPED 3 闪控窗已停止。
IN_SIDEBAR 4 闪控窗在侧边栏中。
IN_FLOATING_BALL 5 闪控窗切换为闪控球。
ERROR 6 闪控窗发生异常。

FloatViewRectChangeInfo

Phone26.0.0+PC/2in126.0.0+Tablet26.0.0+TV26.0.0+Wearable26.0.0+

闪控窗矩形区域变化信息。

起始版本: 26.0.0

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Window.SessionManager

展开
名称 类型 只读 可选 说明
windowRect window.Rect 闪控窗窗口矩形区域。
windowScale number 闪控窗窗口缩放比例。
reason string

闪控窗矩形区域变化的原因。原因和对应含义如下:

"POSITION_CHANGE":位置变化

"SIZE_CHANGE":大小变化

"RECT_CHANGE":位置大小同时变化

在 API参考 中进行搜索
请输入您想要搜索的关键词