监听Ability生命周期

监听Ability生命周期

HarmonyOS
2024-05-22 22:18:27
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
hflame

监听ability生命周期,然后在对应周期触发时,进行自定义操作,如统一日志等。

使用的核心API

ApplicationContext.on(type: ‘abilityLifecycle’, callback: AbilityLifecycleCallback): number;

核心代码解释

生命周期的监听只需要调用该接口传入对象参数即可,这里主要体现想要监听ability的onCreate生命周期,日常的操作都是基于ability做的,导致如果在ability里增加这个监听操作,无法监听到ability的onCreate的生命周期,这里引出AbilityStage解决该问题。

核心代码如下:

//src/main/ets/entryability/StageAbility.ets 
import AbilityStage from '@ohos.app.ability.AbilityStage'; 
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback'; 
 
// 声明ability生命周期回调,需配置所有回调后才可以在applicationContext注册 
let abilityLifecycleCallback:AbilityLifecycleCallback = { 
  onAbilityCreate(ability){ 
    console.warn(`AbilityLifecycleCallback onAbilityCreate.javaleetest ability is ${JSON.stringify(ability)}`); 
  }, 
  onWindowStageCreate(ability, windowStage){ 
    console.warn(`AbilityLifecycleCallback onWindowStageCreate.javaleetest ability is ${JSON.stringify(ability)}, windowStage is is ${JSON.stringify(windowStage)}`); 
  }, 
  onWindowStageActive(ability, windowStage){ 
    console.warn(`AbilityLifecycleCallback onWindowStageActive.javaleetest ability is ${JSON.stringify(ability)}, windowStage is is ${JSON.stringify(windowStage)}`); 
  }, 
  onWindowStageInactive(ability, windowStage){ 
    console.warn(`AbilityLifecycleCallback onWindowStageInactive.javaleetest ability is ${JSON.stringify(ability)}, windowStage is is ${JSON.stringify(windowStage)}`); 
  }, 
  onWindowStageDestroy(ability, windowStage){ 
    // 
    console.warn(`AbilityLifecycleCallback onWindowStageDestroy.javaleetest ability is ${JSON.stringify(ability)}, windowStage is is ${JSON.stringify(windowStage)}`); 
  }, 
  onAbilityDestroy(ability){ 
    console.warn(`AbilityLifecycleCallback onAbilityDestroy.javaleetest ability is ${JSON.stringify(ability)}`); 
  }, 
  onAbilityForeground(ability){ 
    console.warn(`AbilityLifecycleCallback onAbilityForeground.javaleetest ability is ${JSON.stringify(ability)}`); 
  }, 
  onAbilityBackground(ability){ 
    // 
    console.warn(`AbilityLifecycleCallback onAbilityBackground.javaleetest ability is ${JSON.stringify(ability)}`); 
  }, 
  onAbilityContinue(ability){ 
    console.warn(`AbilityLifecycleCallback onAbilityContinue.javaleetest ability is ${JSON.stringify(ability)}`); 
  } 
}; 
 
export default class StageAbility extends AbilityStage{ 
  public static  lifecycleId : number; 
  onCreate() { 
    // 1.通过context属性获取applicationContext 
    let applicationContext = this.context.getApplicationContext(); 
    // 2.通过applicationContext注册监听应用内生命周期 
    try { 
      StageAbility.lifecycleId = applicationContext.on('abilityLifecycle', abilityLifecycleCallback); 
      console.info(`registerAbilityLifecycleCallback lifecycleId: ${StageAbility.lifecycleId}`); 
    } catch (paramError) { 
      console.error(`error: ${paramError.code}, ${paramError.message}`); 
    } 
  } 
};
//src/main/ets/entryability1/EntryAbility1.ets 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import hilog from '@ohos.hilog'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import window from '@ohos.window'; 
import StageAbility from '../entryability/StageAbility'; 
 
export default class EntryAbility1 extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 
    hilog.info(0x0000, 'AbilityLifecycleCallback', '%{public}s', 'AbilityLifecycleCallback - Ability onCreate'); 
  } 
 
  onDestroy() { 
    //应用退出时,通过监听创建时的返回值lifecycleId结束对ability生命周期的监听, 
    let applicationContext = this.context.getApplicationContext(); 
    applicationContext.off("abilityLifecycle",StageAbility.lifecycleId) 
    hilog.info(0x0000, 'AbilityLifecycleCallback', '%{public}s', 'Ability onDestroy'); 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage) { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
    windowStage.loadContent('pages/Index', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  } 
 
  onWindowStageDestroy() { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
 
  onForeground() { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
 
  onBackground() { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
}

总结:

实现效果

注明适配的版本信息

  • IDE:DevEco    Studio 4.0.3.600
  • SDK:HarmoneyOS    4.0.10.11
分享
微博
QQ
微信
回复
2024-05-23 15:48:50
相关问题
swiper切换监听生命周期
477浏览 • 1回复 待解决
如何监听AbilitySlice的生命周期
3965浏览 • 1回复 待解决
Ability生命周期回调函数有哪些呢?
4218浏览 • 1回复 待解决
弹窗组件无法调用生命周期接口
745浏览 • 1回复 待解决
如何使用AbilityStage的生命周期函数
815浏览 • 1回复 待解决
JMS 消息怎么设置生命周期呢?
1502浏览 • 1回复 待解决
如何理解和重写onBackPress生命周期
854浏览 • 1回复 待解决
公共事件的生命周期是怎样的
542浏览 • 1回复 待解决