智能客服
你问我答,随时在线为你解决问题



流程说明:
以下是应用内开启/关闭系统未成年人模式相关接口说明,更多接口及使用方法请参见API参考。
接口名 | 描述 |
|---|---|
leadToTurnOnMinorsMode(context: common.Context): Promise<void> | 调用该方法进行开启未成年人模式流程。 |
leadToTurnOffMinorsMode(context: common.Context): Promise<void> | 调用该方法进行关闭未成年人模式流程。 |
同步接口,获取未成年人模式的开启状态,以及年龄段信息。 | |
getMinorsProtectionInfo(): Promise<MinorsProtectionInfo> | 异步接口,获取未成年人模式的开启状态,以及年龄段信息。 |
verifyMinorsProtectionCredential(context: common.Context): Promise<boolean> | 调用该方法拉起验证未成年人模式密码页面。 |
以下是未成年人模式开启/关闭发送的广播事件。
事件名称 | 值 | 描述 |
|---|---|---|
usual.event.MINORSMODE_ON | 表示未成年人模式开启动作。 | |
usual.event.MINORSMODE_OFF | 表示未成年人模式关闭动作。 |
在进行代码开发前,请先确认您已完成配置Client ID工作。
import { minorsProtection } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
// 以上引入的模块为当前场景的全量模块,请按照具体实现按需引入//订阅者信息
const subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
events: [commonEventManager.Support.COMMON_EVENT_MINORSMODE_ON,commonEventManager.Support.COMMON_EVENT_MINORSMODE_OFF]
};
// 如开发者使用await改写createSubscriber方法,需要把此变量定义到全局(struct外层)
let subscriber: commonEventManager.CommonEventSubscriber;
//创建订阅者
commonEventManager.createSubscriber(subscribeInfo)
.then((commonEventSubscriber: commonEventManager.CommonEventSubscriber) => {
// 这里获取到commonEventSubscriber对象需要暂存,用于后续事件回调。不可直接使用,否则会出现事件回调不生效的情况
subscriber = commonEventSubscriber;
//订阅公共事件
commonEventManager.subscribe(subscriber,
(error: BusinessError, data: commonEventManager.CommonEventData) => {
if (error) {
this.dealCommonEventAllError(error);
return;
}
if (data.event === commonEventManager.Support.COMMON_EVENT_MINORSMODE_ON) {
// 订阅到开启事件,可以调用获取年龄段的接口,根据年龄段刷新内容展示,同时如开发者有缓存年龄段或未成年人模式开启状态,则需要刷新缓存
return;
}
if (data.event === commonEventManager.Support.COMMON_EVENT_MINORSMODE_OFF) {
// 订阅到关闭事件,关闭当前应用的未成年人模式,刷新应用内容展示,取消年龄限制,如开发者有缓存未成年人模式开启状态,则需要刷新缓存
}
});
})
.catch((error: BusinessError) => {
this.dealCommonEventAllError(error);
}); dealCommonEventAllError(error: BusinessError): void {
hilog.error(0x0000, 'testTag', `Failed to subscribe. Code: ${error.code}, message: ${error.message}`);
} 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.');
}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}`);
} if (canIUse('SystemCapability.AuthenticationServices.HuaweiID.MinorsProtection')) {
try {
if (minorsProtection.supportMinorsMode()) {
minorsProtection.leadToTurnOffMinorsMode(getContext(this))
.then(() => {
// 接口调用完成,如需显示弹窗,请在此处处理
})
.catch((error: BusinessError<Object>) => {
this.dealTurnOffAllError(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 leadToTurnOffMinorsMode interface.');
} dealTurnOffAllError(error: BusinessError<Object>): void {
hilog.error(0x0000, 'testTag', `Failed to leadToTurnOffMinorsMode. Code: ${error.code}, message: ${error.message}`);
}if (canIUse('SystemCapability.AuthenticationServices.HuaweiID.MinorsProtection')) {
try {
if (minorsProtection.supportMinorsMode()) {
minorsProtection.leadToTurnOnMinorsMode(getContext(this))
.then(() => {
// 接口调用完成,如需显示弹窗,请在此处处理
})
.catch((error: BusinessError<Object>) => {
this.dealTurnOnAllError(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 leadToTurnOnMinorsMode interface.');
} dealTurnOnAllError(error: BusinessError<Object>): void {
hilog.error(0x0000, 'testTag', `Failed to leadToTurnOnMinorsMode. Code: ${error.code}, message: ${error.message}`);
}if (canIUse('SystemCapability.AuthenticationServices.HuaweiID.MinorsProtection')) {
try {
if (minorsProtection.supportMinorsMode()) {
minorsProtection.verifyMinorsProtectionCredential(getContext(this))
.then((result: boolean) => {
hilog.info(0x0000, 'testTag', `Succeeded in getting verify result is: ${result.valueOf()}`);
// 使用结果判断验密是否通过,执行后续流程
})
.catch((error: BusinessError<Object>) => {
this.dealVerifyAllError(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 verifyMinorsProtectionCredential interface.');
} dealVerifyAllError(error: BusinessError<Object>): void {
hilog.error(0x0000, 'testTag', `Failed to verify. Code: ${error.code}, message: ${error.message}`);
}