鸿蒙开发接口Ability框架:【ApplicationContext】

电子说

1.3w人已加入

描述

ApplicationContext

ApplicationContext模块提供开发者应用级别的的上下文的能力,包括提供注册及取消注册应用内组件生命周期的监听接口。

说明:
开发前请熟悉鸿蒙开发指导文档:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。

使用说明

在使用ApplicationContext的功能前,需要通过context的实例获取。

let applicationContext = this.context.getApplicationContext();

ApplicationContext.registerAbilityLifecycleCallback

registerAbilityLifecycleCallback(callback: AbilityLifecycleCallback): number ;

注册监听应用内生命周期

系统能力 :SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
[AbilityLifecycleCallback]callback回调方法,返回注册监听事件的id

返回值:

类型说明
number返回的此次注册监听生命周期的id(每次注册该id会自增+1,当超过监听上限数量2^63-1时,返回-1)

示例:

import AbilityStage from "@ohos.application.AbilityStage";

var lifecycleid;

export default class MyAbilityStage extends AbilityStage {
  onCreate() {
      console.log("MyAbilityStage onCreate")
      let AbilityLifecycleCallback  =  {
          onAbilityCreate(ability){
              console.log("AbilityLifecycleCallback onAbilityCreate ability:" + JSON.stringify(ability));        
          },
          onAbilityWindowStageCreate(ability){
              console.log("AbilityLifecycleCallback onAbilityWindowStageCreate ability:" + JSON.stringify(ability));           
          },
          onAbilityWindowStageDestroy(ability){
              console.log("AbilityLifecycleCallback onAbilityWindowStageDestroy ability:" + JSON.stringify(ability));
          },
          onAbilityDestroy(ability){
              console.log("AbilityLifecycleCallback onAbilityDestroy ability:" + JSON.stringify(ability));             
          },
          onAbilityForeground(ability){
              console.log("AbilityLifecycleCallback onAbilityForeground ability:" + JSON.stringify(ability));             
          },
          onAbilityBackground(ability){
              console.log("AbilityLifecycleCallback onAbilityBackground ability:" + JSON.stringify(ability));              
          },
          onAbilityContinue(ability){
              console.log("AbilityLifecycleCallback onAbilityContinue ability:" + JSON.stringify(ability));
          }
      }
      // 1.通过context属性获取applicationContext
      let applicationContext = this.context.getApplicationContext();
      // 2.通过applicationContext注册监听应用内生命周期
      lifecycleid = applicationContext.registerAbilityLifecycleCallback(AbilityLifecycleCallback);
      console.log("registerAbilityLifecycleCallback number: " + JSON.stringify(lifecycleid));       
  }
  onDestroy() {
      let applicationContext = this.context.getApplicationContext();
      applicationContext.unregisterAbilityLifecycleCallback(lifecycleid, (error, data) = > {
      console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error));
      });
  }
}

ApplicationContext.unregisterAbilityLifecycleCallback

unregisterAbilityLifecycleCallback(callbackId: number , callback: AsyncCallback< void >): void ;

取消监听应用内生命周期

系统能力 :SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名类型必填说明
callbackIdnumber注册监听应用内生命周期的id
AsyncCallbackcallback回调方法

鸿蒙

示例:

let applicationContext = this.context.getApplicationContext();
let lifecycleid=1
console.log("stage applicationContext: " + JSON.stringify(applicationContext));
applicationContext.unregisterAbilityLifecycleCallback(lifecycleid, (error, data) = > {
    console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error));
});

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分