监听Ability生命周期

监听Ability生命周期

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

监听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
相关问题
如何监听全局Ability生命周期
387浏览 • 1回复 待解决
swiper切换监听生命周期
1004浏览 • 1回复 待解决
如何监听subwindow生命周期
378浏览 • 1回复 待解决
如何监听AbilitySlice的生命周期
5132浏览 • 1回复 待解决
HarmonyOS 监听所有page的生命周期
492浏览 • 1回复 待解决
HarmonyOS能否全局监听页面的生命周期
622浏览 • 1回复 待解决
Ability生命周期回调函数有哪些呢?
5729浏览 • 1回复 待解决
HarmonyOS Navigation生命周期
28浏览 • 1回复 待解决
HarmonyOS 模块生命周期管理
246浏览 • 1回复 待解决
Dialog组件生命周期问题
346浏览 • 1回复 待解决
HarmonyOS Navigation生命周期问题
21浏览 • 1回复 待解决
HarmonyOS 生命周期不触发
78浏览 • 1回复 待解决
HarmonyOS NavDestination生命周期问题
27浏览 • 1回复 待解决