HarmoneyOS--Ability(能力)、窗口、通知

标题

文章目录


一、什么是Ability?

typescript 复制代码
开发模式提供的开发功能抽象的描述。
其中重要的是UiAbility,界面组件能力,负责所有界面的处理。
通过配置可以变更单例,多例,指定实例,在module.json5中进行配置
如:
单例:lunchType:'singleton'
多例:lunchType:'standard'
指定实例:lunchType:'specified'

二、使用步骤(单例和多例)

1.单例和多例

typescript 复制代码
操作步骤:
1.在module.json5中进行配置单例、多例、指定实例。
单例:lunchType:'singleton'
多例:lunchType:'standard'
	"abilities": [
      {"launchType": "singleton"}
     ]
2.唤起页面:
	//2.1 链接上下文
	  private cont = getContext(this) as common.UIAbilityContext
	//2.2 定义参数Want
	const want: Want = {
      // deviceId:'', 标识ID,可选
      bundleName: 'com.www.myapplication', //包名: appScope
      moduleName: 'entry', // 模块名称 module.json5
      abilityName: 'EntryAbilityNext', //应用名称  module.json5
      parameters: {} // 参数
    }
	//2.3 拉起页面
	con.startAbility(want)


3.若传递了参数,则需要在拉起页面中的ability中进行接收
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.getParams(want)
  }
  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.getParams(want)
  }

  getParams(want: Want) {
    const par = want.parameters as Record<string, string>
    AppStorage.setOrCreate('message', par.message)
  }

2.指定实例

typescript 复制代码
操作步骤:
思路:
// 子模块配置指定实例
// 链接上下文
// 配置Want
// 调用函数并传参,结束接收数据
// 创建MyAbilityStage , 并重写函数onAcceptWant
// 配置module.json 定义路径
// 在子模块UiAbility接收参数(onCreat和onNewWant)

1.在module.json5中进行配置单例、多例、指定实例。
	"abilities": [
	指定实例:lunchType:'specified'
      {"launchType": "specified"}
     ]
2.唤起页面:
	//2.1 链接上下文
	  private cont = getContext(this) as common.UIAbilityContext
	//2.2 定义参数Want
	//2.3 拉起页面
	this.cont.startAbilityForResult({ 
            bundleName: 'com.www.myapplication', //包名: appScope
            moduleName: 'OtherApplication',  // 模块名称 module.json5
            abilityName: 'OtherApplicationAbility',  // 模块名称 module.json5
            parameters: {
              message: '传递子给的模块数据'  // 参数
              key:id // 用于指定实例
            }
          })
            .then((res) => {
            // 返回的数据
              const data = res.want?.parameters as Record<string, string>
              console.log('system===>回调接收数据' + JSON.stringify(data.message))
              this.message = data.message
            }).catch((err: BusinessError) => {
            promptAction.showToast({ message: '开启实例异常' + JSON.stringify(err) })
          })


3.若传递了参数,则需要在拉起页面中的ability中进行接收
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.getParams(want)
  }
  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    this.getParams(want)
  }

  getParams(want: Want) {
    const par = want.parameters as Record<string, string>
    AppStorage.setOrCreate('message', par.message)
  }
4. 在拉起页面创建MyAbilityStage类
	export default class MyAbilityStage extends AbilityStage {
  // 重写函数
  override onAcceptWant(want: Want) {
    if (want.abilityName === 'InnerEntryAbility') {
      console.log('system===>重写' + want.abilityName)
      const param = want.parameters as Record<string, string>
      return 'InnerEntryAbility' + param.key
    }
    return ''
  }
}
5.在module.json5中配置,见下面
 "srcEntry": "./ets/abilityStage/MyAbilityStage.ets",


6.在拉起页面编辑往主模块返回数据
// 链接上下文
          const cont = getContext(this) as common.UIAbilityContext
          // want :拉起主应用
          // 调用函数,返回结果并可以关闭当前应用
          cont.terminateSelfWithResult({ // 返回参数使用的
            resultCode: 200, want: {
              bundleName: 'com.www.myapplication',
              moduleName: 'entry',
              abilityName: 'EntryAbility',
              parameters: {
                message: '子模块数据处理完成'
              }
            }
          }, () => {
            // 关闭应用
          })

三、窗口

思路:

1.ability中初始化生命周期将windowStage保存到全局应用中。

2.在需要开启子窗口的位置,设置大小,位置,加载页面遮罩层等UI,并接收window结果值保存到全局应用中

3.在小窗口位置调用window,执行关闭并操作其他业务(关闭主窗口的遮罩层,则需要进行使用订阅)

1.ability中初始化生命周期将windowStage保存到全局应用中。

typescript 复制代码
onWindowStageCreate(windowStage: window.WindowStage): void {
	AppStorage.setOrCreate('windowStage',windowStage)
}

2.在需要开启子窗口的位置,设置大小,位置,加载页面遮罩层等UI,并接收window结果值保存到全局应用中

typescript 复制代码
1.变量
  // 读取全局应用状态
  private windowStage: window.WindowStage | undefined = AppStorage.get('windowStage')
  // 遮罩层
  @State isShow: boolean = false
	

2.遮罩层UI
if (this.isShow) {
        Rect()
          .width('100%')
          .height('100%')
          .backgroundColor("#ffddecec")
          .opacity(0.2)
      }


3.设置子窗口大小,位置:
// 弹出小窗口 .并将数据保存到应用状态管理中
this.windowStage?.createSubWindow('dLog').then((win: window.Window) => {
   const system = display.getDefaultDisplaySync() // 获取系统的数据,获取到宽高
   // 画小弹框的大小
   win.resize(system.width * 0.8, system.height * 0.2)
   // 所在位置
   win.moveWindowTo(system.width / 9, system.height - (system.height / 4))
   
   this.isShow = true
   //设置加载路径
   win.setUIContent('pages/CodePage', () => { // 需要重新创建一个entry
     //开启弹框
     win.showWindow().then(() => {})
   })
   // 保存应用
   AppStorage.setOrCreate('window', win)
 })

3.在小窗口位置调用window,执行关闭并操作其他业务(关闭主窗口的遮罩层,则需要进行使用订阅)

typescript 复制代码
  private win: window.Window | undefined = AppStorage.get('window')

//关闭窗口并关闭主窗口遮罩层(小窗口发布)
  this.win?.destroyWindow(()=>{
    getContext(this).eventHub.emit('close') // 点击取消或者确定使用订阅关闭遮罩层
    router.replaceUrl({url:'pages/HomePage'}) // 只有在会调用才可以进行跳转,同步操作
  })


// 主窗口进行订阅,并执行相关操作
  // 生命周期加载订阅
  aboutToAppear(): void {
    getContext(this).eventHub.on('close', () => {
      this.isShow = false // 关闭遮罩层
    })
  }

四、通知

typescript 复制代码
  // 开启通知,在组件加载的时候
  async getNotification() {
    let context = getContext(this) as common.UIAbilityContext;
    let result = await notificationManager.isNotificationEnabled()
    if (!result) {
      notificationManager.requestEnableNotification(context)
    }
  }
相关推荐
猫林老师2 天前
HarmonyOS数据持久化:Preferences轻量级存储实战
华为·harmonyos
Devil枫3 天前
鸿蒙深链落地实战:从安全解析到异常兜底的全链路设计
安全·华为·harmonyos
广州腾科助你拿下华为认证3 天前
华为考试:HCIE数通考试难度分析
大数据·华为
与天仙漫步星海3 天前
华为基本命令
华为
低调小一3 天前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
猛码Memmat3 天前
华为HarmonyOS开发文档
华为·harmonyos
流影ng3 天前
【HarmonyOS】MVVM与三层架构
华为·架构·harmonyos
爱笑的眼睛113 天前
HarmonyOS Stage 模型与 ArkUI 声明式开发深度实践:构建高效稳定的应用
华为·harmonyos
安卓开发者3 天前
鸿蒙Next ArkWeb网页文件上传与下载完全指南
华为·harmonyos