通过同步方式,调用getMinorsProtectionInfoSync获取未成年人模式的开启状态,以及年龄段信息。if (canIUse('SystemCapability.AuthenticationServices.HuaweiID.MinorsProtection')) {
try {
if (minorsProtection.supportMinorsMode()) {
const minorsProtectionInfo: minorsProtection.MinorsProtectionInfo =
minorsProtection.getMinorsProtectionInfoSync();
// 获取未成年人模式开启状态
const minorsProtectionMode: boolean = minorsProtectionInfo.minorsProtectionMode;
hilog.info(0x0000, 'testTag', `Succeeded in getting minorsProtectionMode is: ${minorsProtectionMode.valueOf()}`);
// 未成年人模式已开启,获取年龄段信息
if (minorsProtectionMode) {
const ageGroup: minorsProtection.AgeGroup | undefined = minorsProtectionInfo.ageGroup;
// 如开发者有频繁使用到未成年人模式开启状态,这里则需缓存未成年人模式开启状态
if (ageGroup) {
hilog.info(0x0000, 'testTag', `Succeeded in getting lowerAge is: ${ageGroup.lowerAge}`);
hilog.info(0x0000, 'testTag', `Succeeded in getting upperAge is: ${ageGroup.upperAge}`);
// 根据年龄段刷新内容展示。如开发者有频繁使用到年龄段信息,这里则需缓存年龄段信息
}
} else {
// 未成年人模式未开启,应用需跟随系统未成年人模式,展示内容不做限制
}
} else {
hilog.info(0x0000, 'testTag',
'The current device environment does not support the youth mode, please check the current device environment.');
}
} catch (error) {
hilog.error(0x0000, 'testTag',
`Failed to invoke supportMinorsMode or getMinorsProtectionInfoSync. errCode: ${error.code}, errMessage: ${error.message}`);
}
} else {
hilog.info(0x0000, 'testTag',
'The current device does not support the invoking of the getMinorsProtectionInfoSync interface.');
}
通过Promise异步回调方式,调用getMinorsProtectionInfo获取未成年人模式的开启状态,以及年龄段信息。if (canIUse('SystemCapability.AuthenticationServices.HuaweiID.MinorsProtection')) {
try {
if (minorsProtection.supportMinorsMode()) {
minorsProtection.getMinorsProtectionInfo()
.then((minorsProtectionInfo: minorsProtection.MinorsProtectionInfo) => {
// 获取未成年人模式开启状态
const minorsProtectionMode: boolean = minorsProtectionInfo.minorsProtectionMode;
// 如开发者有频繁使用到未成年人模式开启状态,这里则需缓存未成年人模式开启状态
hilog.info(0x0000, 'testTag',
`Succeeded in getting minorsProtectionMode is: ${minorsProtectionMode.valueOf()}`);
// 未成年人模式已开启,获取年龄段信息
if (minorsProtectionMode) {
const ageGroup: minorsProtection.AgeGroup | undefined = minorsProtectionInfo.ageGroup;
if (ageGroup) {
hilog.info(0x0000, 'testTag', `Succeeded in getting lowerAge is: ${ageGroup.lowerAge}`);
hilog.info(0x0000, 'testTag', `Succeeded in getting upperAge is: ${ageGroup.upperAge}`);
// 根据年龄段刷新内容展示。如开发者有频繁使用到年龄段信息,这里则需缓存年龄段信息
}
} else {
// 未成年人模式未开启,应用需跟随系统未成年人模式,展示内容不做限制
}
})
.catch((error: BusinessError<Object>) => {
this.dealGetMinorsInfoAllError(error);
});
} else {
hilog.info(0x0000, 'testTag',
'The current device environment does not support the youth mode, please check the current device environment.');
}
} catch (error) {
hilog.error(0x0000, 'testTag',
`Failed to invoke supportMinorsMode. errCode: ${error.code}, errMessage: ${error.message}`);
}
} else {
hilog.info(0x0000, 'testTag',
'The current device does not support the invoking of the getMinorsProtectionInfo interface.');
}
dealGetMinorsInfoAllError(error: BusinessError<Object>): void {
hilog.error(0x0000, 'testTag', `Failed to getMinorsProtectionInfo. Code: ${error.code}, message: ${error.message}`);
}