请参考如下代码import { Context } from '@kit.AbilityKit'; export class GlobalContext { private constructor() {} private static instance: GlobalContext; private context:Context | undefined public static getInstance(): GlobalContext { if (!GlobalContext.instance) { GlobalContext.instance = new GlobalContext(); } return GlobalContext.instance; } setContext(contex:Context){ this.context = contex } getContext(): Context | undefined { return this.context } }在EntryAbility初始化设置contextasync onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): Promise<void> { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); GlobalContext.getInstance().setContext(getContext(this)); }
请参考如下代码
在EntryAbility初始化设置context