未成年人模式护航,保障安全健康上网

为保护未成年人的上网环境,预防未成年人沉迷网络,帮助未成年人培养积极健康的用网习惯,HarmonyOS SDK 提供未成年人模式功能,在华为设备上加强对面向未成年人的产品和服务的管理。

场景介绍(应用跟随系统未成年人模式状态变化)

1.查询系统状态:建议应用跟随系统未成年人模式状态切换,随系统一同开启或关闭未成年人模式。应用启动时可以查询系统的未成年人模式是否开启。未成年人模式开启时,应用应主动切换为未成年人模式。

2.感知系统状态切换:应用可通过订阅公共事件感知未成年人模式状态变化,应用进程存在时,如果用户切换系统的未成年人模式状态(开启或关闭),应用可主动跟随系统进行切换。

3.验证家长密码:当用户在调整应用内未成年人模式设置(如内容偏好等)时,应用可调用验证未成年人模式密码接口,让用户验证未成年人模式的家长密码,验证成功后可以进行调整。

开发步骤

1.准备:导入minorsProtection模块及相关公共模块。

复制代码
import { minorsProtection } from '@kit.AccountKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
// 以上引入的模块为当前场景的全量模块,请按照具体实现按需引入

2.感知系统状态切换:创建订阅者,订阅系统未成年人模式开启/关闭事件。推荐在应用Ability的onCreate生命周期中调用。

复制代码
//订阅者信息。
let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
  events: [commonEventManager.Support.COMMON_EVENT_MINORSMODE_ON,commonEventManager.Support.COMMON_EVENT_MINORSMODE_OFF]
};
let subscriber: commonEventManager.CommonEventSubscriber;

//创建订阅者。
commonEventManager.createSubscriber(subscribeInfo)
  .then((commonEventSubscriber: commonEventManager.CommonEventSubscriber) => {
    subscriber = commonEventSubscriber;
    //订阅公共事件。
    commonEventManager.subscribe(subscriber,
      (error: BusinessError, data: commonEventManager.CommonEventData) => {
        if (error) {
          this.dealCommonEventAllError(error);
        } else {
          if (data.event === commonEventManager.Support.COMMON_EVENT_MINORSMODE_ON) {
            // 订阅到开启事件,可以调用查询年龄段接口
          }
          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}`);
}

3.查询系统状态:开发者可选择以下一种方式获取未成年人模式的开启状态,以及年龄段信息。推荐在自定义组件的aboutToAppear生命周期或者应用Ability的onCreate生命周期中调用。

当应用期望立即获取结果时,推荐使用同步方式;当应用期望使用非阻塞的方式调用接口时,推荐使用Promise异步回调方式。

使用同步方式,调用getMinorsProtectionInfoSync获取未成年人模式的开启状态,以及年龄段信息。

复制代码
if (canIUse('SystemCapability.AuthenticationServices.HuaweiID.MinorsProtection')) {
  const minorsProtectionInfo: minorsProtection.MinorsProtectionInfo =
    minorsProtection.getMinorsProtectionInfoSync();
  // 获取未成年人模式开启状态
  const minorsProtectionMode: boolean = minorsProtectionInfo.minorsProtectionMode;
  hilog.info(0x0000, 'testTag', 'Succeeded in getting minorsProtectionMode is: %{public}s',
    minorsProtectionMode.valueOf());
  // 未成年人模式已开启,获取年龄段信息
  if (minorsProtectionMode) {
    const ageGroup: minorsProtection.AgeGroup | undefined = minorsProtectionInfo.ageGroup;
    if (ageGroup) {
      hilog.info(0x0000, 'testTag', 'Succeeded in getting lowerAge is: %{public}s', ageGroup.lowerAge.toString());
      hilog.info(0x0000, 'testTag', 'Succeeded in getting upperAge is: %{public}s', ageGroup.upperAge.toString());
    }
  } else {
    // 未成年人模式未开启,建议应用跟随系统未成年人模式,展示正常内容
  }
} 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')) {
  minorsProtection.getMinorsProtectionInfo()
    .then((minorsProtectionInfo: minorsProtection.MinorsProtectionInfo) => {
      // 获取未成年人模式开启状态
      const minorsProtectionMode: boolean = minorsProtectionInfo.minorsProtectionMode;
      hilog.info(0x0000, 'testTag', 'Succeeded in getting minorsProtectionMode is: %{public}s',
        minorsProtectionMode.valueOf());
      // 未成年人模式已开启,获取年龄段信息
      if (minorsProtectionMode) {
        const ageGroup: minorsProtection.AgeGroup | undefined = minorsProtectionInfo.ageGroup;
        if (ageGroup) {
          hilog.info(0x0000, 'testTag', 'Succeeded in getting lowerAge is: %{public}s', ageGroup.lowerAge.toString());
          hilog.info(0x0000, 'testTag', 'Succeeded in getting upperAge is: %{public}s', ageGroup.upperAge.toString());
        }
      } else {
        // 未成年人模式未开启,建议应用跟随系统未成年人模式,展示正常内容
      }
    })
    .catch((error: BusinessError<object>) =&gt; {
      this.dealGetMinorsInfoAllError(error);
    });
} 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}`);
}

验证家长密码:未成年人模式开启时,如果用户需要调整应用内未成年人模式设置(如内容偏好等),可调用verifyMinorsProtectionCredential方法拉起验证未成年人模式的家长密码页面。

复制代码
if (canIUse('SystemCapability.AuthenticationServices.HuaweiID.MinorsProtection')) {
  minorsProtection.verifyMinorsProtectionCredential(getContext(this))
    .then((result: boolean) =&gt; {
      hilog.info(0x0000, 'testTag', 'Succeeded in getting verify result is: %{public}s', result.valueOf());
      // 使用结果判断验密是否通过,执行后续流程
    })
    .catch((error: BusinessError<object>) =&gt; {
      this.dealVerifyAllError(error);
    });
} 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}`);
}

了解更多详情>>

获取未成年人模式服务开发指导文档

相关推荐
森语林溪5 分钟前
大数据环境搭建从零开始(十七):JDK 17 安装与配置完整指南
java·大数据·开发语言·centos·vmware·软件需求·虚拟机
合作小小程序员小小店12 分钟前
web安全开发,在线%服务器日志入侵检测%系统安全开发,基于Python,flaskWeb,正则表达式检测,mysql数据库
服务器·python·安全·web安全·flask·安全威胁分析·安全架构
赵得C18 分钟前
智能体的范式革命:华为全栈技术链驱动下一代AI Agent
人工智能·华为·ai·ai编程
lsx20240634 分钟前
HTML 音频(Audio)详解
开发语言
woshihonghonga37 分钟前
【动手学深度学习】
开发语言·python
威风的虫1 小时前
ES6 数组方法:告别循环,拥抱函数式编程
开发语言·前端·javascript
码界筑梦坊1 小时前
240-基于Python的医疗疾病数据可视化分析系统
开发语言·python·信息可视化·数据分析·毕业设计·echarts
2301_803554521 小时前
C++ 锁类型大全详解
开发语言·c++
wuwu_q1 小时前
用通俗易懂方式,详细讲讲 Kotlin Flow 中的 map 操作符
android·开发语言·kotlin
曼巴UE51 小时前
UE5 C++ Slate 画曲线
开发语言·c++·ue5