1# 锁屏管理服务 2 3## 简介 4### 内容介绍 5锁屏管理服务是OpenHarmony中系统服务,为锁屏应用提供注册亮屏、灭屏、开启屏幕、结束休眠、退出动画、请求解锁结果监听,并提供回调结果给锁屏应用。锁屏管理服务向三方应用提供请求解锁、查询锁屏状态、查询是否设置锁屏密码的能力。 6 7**图 1** 架构图 8 9 10 11### 框架图介绍 121.三方应用支持操作请求解锁、查询锁屏状态、查询是否设置锁屏密码接口调用。\ 132.锁屏应用注册亮屏、灭屏、开启屏幕、结束休眠、退出动画、请求解锁结果监听等事件 \ 143.框架层API用来处理三方应用和锁屏应用的js接口请求处理,NAPI层进行js调用的处理 \ 154.框架层IDL用来处理NAPI接口向锁屏管理服务之间的桥梁,进行IPC通讯 \ 165.锁屏管理服务用来处理三方应用和锁屏应用接口请求,并作出对应处理,提供相应的返回结果。 17 18## 目录 19 20``` 21/base/theme/screenlock_mgr 22├── figures # 构架图 23├── frameworks 24│ ├── js/napi # js接口解析成napi接口 25│ └── native # 对客户端提供的接口 26├── sa_profile # 组件包含的系统服务的配置文件和进程的配置文件 27├── services # 锁屏管理服务实现 28├── test # 接口的单元测试 29└── utils # 组件包含日志打印和有序公共事件定义的常量 30``` 31 32## 说明 33 34### 接口说明 35 36**表 1** 锁屏管理服务的主要方法说明 37 38| 接口名 | 描述 | 39| -------------------------- | -------------------------- | 40| isScreenLocked(callback: AsyncCallback<boolean>): void; | 判断屏幕是否锁屏。callback方式 | 41| isScreenLocked(): Promise<boolean>; | 判断屏幕是否锁屏。Promise方式 | 42| isLocked(): boolean; | 判断屏幕是否锁屏。返回true表示屏幕已锁屏;返回false表示屏幕未锁屏。同步方式 | 43| isSecureMode(callback: AsyncCallback<boolean>): void; | 判断当前设备的屏幕锁定是否安全(安全屏幕锁定意味着解锁屏幕需要密码、图案或其他用户身份识别)。callback方式 | 44| isSecureMode(): Promise<boolean>; | 判断当前设备的屏幕锁定是否安全(安全屏幕锁定意味着解锁屏幕需要密码、图案或其他用户身份识别)。Promise方式 | 45| isSecure(): boolean; | 判断当前设备的屏幕锁定是否安全(安全屏幕锁定意味着解锁屏幕需要密码、图案或其他用户身份识别)。返回true表示当前设备的屏幕锁定安全;返回false表示当前设备的屏幕锁定不安全。同步方式 | 46| unlockScreen(callback: AsyncCallback<void>): void; | 三方应用解锁屏幕。callback方式 | 47| unlockScreen(): Promise<void>; | 三方应用解锁屏幕。Promise方式 | 48| unlock(callback: AsyncCallback<boolean>): void; | 三方应用解锁屏幕。如果屏幕解锁成功,则返回true,否则返回false。callback方式 | 49| unlock():Promise<boolean>; | 三方应用解锁屏幕。如果屏幕解锁成功,则返回true,否则返回false。Promise方式 | 50| lock(callback: AsyncCallback<boolean>): void; | 系统API,锁定屏幕。如果屏幕锁定成功,则返回true,否则返回false。callback方式 | 51| lock():Promise<boolean>; | 系统API,锁定屏幕。如果屏幕锁定成功,则返回true,否则返回false。Promise方式 | 52| SystemEvent { eventType: EventType, params: string } | 定义了系统事件回调参数结构,包含事件类型以及string类型的参数 | 53| onSystemEvent(callback: Callback<SystemEvent>): boolean; | 系统API,注册与系统屏幕锁定相关的系统事件。如果注册系统事件成功,则返回true,否则返回false。callback方式 | 54| sendScreenLockEvent(event: String, parameter: number, callback: AsyncCallback<boolean>): void; | 系统API,锁屏应用给锁屏管理服务发送事件。callback方式 | 55| sendScreenLockEvent(event: String, parameter: number): Promise<boolean>; | 系统API,锁屏应用给锁屏管理服务发送事件。promise方式 | 56 57 58**表 2** EventType-事件类型说明 59 60| 事件类型 | 描述 | 61| -------------------------- | -------------------------- | 62| beginWakeUp | 表示设备开始唤醒。 | 63| endWakeUp | 表示设备结束唤醒。 | 64| beginScreenOn | 表示设备开始亮屏。 | 65| endScreenOn | 表示设备结束亮屏。 | 66| beginScreenOff | 表示设备开始灭屏。 | 67| endScreenOff | 表示设备结束灭屏。 | 68| unlockScreen | 表示请求屏幕解锁。 | 69| lockScreen | 表示请求屏幕锁定。 | 70| beginExitAnimation | 表示开始退场动画。 | 71| beginSleep | 表示设备开始休眠。 | 72| endSleep | 表示设备结束休眠。 | 73| changeUser | 表示切换用户。 | 74| screenlockEnabled | 表示锁屏是否启用。 | 75| serviceRestart | 表示锁屏服务进行重启。 | 76 77### JS 接口使用示例 78 79三方应用向锁屏管理服务进行查询屏幕锁屏状态 80 81```js 82导入模块 83import screenLock from '@ohos.screenlock'; 84 85// Promise方式,在异步回调里面获取锁屏状态结果 86screenLock.isScreenLocked() 87 .then((data) => { 88 // 异步回调打印查询锁屏状态的结果 89 console.log(`Obtain whether the screen is locked successfully. result: ${data}`); 90 }).catch((err) => { 91 // 打印错误信息 92 console.error(`Failed to obtain whether the screen is locked, because: ${err.message}`) 93}); 94 95// callback方式,在异步回调里面获取锁屏状态结果 96screenLock.isScreenLocked((err, data) => { 97 if (err) { 98 // 打印错误信息 99 console.error(`Failed to obtain whether the screen is locked, because: ${err.message}`) 100 return; 101 } 102 // 打印查询锁屏状态的结果 103 console.log(`Obtain whether the screen is locked successfully. result: ${data}`); 104}); 105 106 // 同步方式里面获取锁屏状态结果。如果屏幕当前已锁定,则返回true,否则返回false 107let isLocked = screenLock.isLocked(); 108 109``` 110 111判断当前设备的屏幕锁定是否安全 112 113```js 114 115// Promise方式,在异步回调里面获取当前设备的屏幕锁定是否安全结果 116screenLock.isSecureMode().then((data) => { 117 console.log(`Obtain whether the device is in secure mode successfully. result: ${data}`); 118}).catch((err) => { 119 console.error(`Failed to obtain whether the device is in secure mode, because: ${err.message}`); 120}); 121 122// callback方式,在异步回调里面获取当前设备的屏幕锁定是否安全结果 123screenLock.isSecureMode((err, data)=>{ 124 if (err) { 125 console.error(`Failed to obtain whether the device is in secure mode, because: ${err.message}`); 126 return; 127 } 128 console.info(`Obtain whether the device is in secure mode successfully. result: ${data}`); 129}); 130 131 // 同步方式里面获取当前设备的屏幕锁定是否安全结果。如果当前设备的屏幕锁定安全,则返回true,否则返回false 132let isSecure = screenLock.isSecure(); 133 134``` 135 136锁定屏幕 137 138```js 139 140// Promise方式,在异步回调里面获取锁屏是否成功的结果 141screenLock.lock().then((data) => { 142 console.log(`lock the screen successfully. result: ${data}`); 143}).catch((err) => { 144 console.error(`Failed to lock the screen, because: ${err.message}`); 145}); 146 147// callback方式,在异步回调里面获取锁屏是否成功的结果 148screenLock.lock((err, data) => { 149 if (err) { 150 console.error(`Failed to lock the screen, because: ${err.message}`); 151 return; 152 } 153 console.info(`lock the screen successfully. result: ${data}`); 154}); 155 156``` 157 158锁屏应用注册事件说明:锁屏应用向锁屏管理服务注册相关监听事件 159 160 ```js 161try { 162 let isSuccess = screenLock.onSystemEvent((event) => { 163 console.log(`Register the system event which related to screenlock successfully. eventType: ${event.eventType}`) 164 }); 165} catch (err) { 166 console.error(`Failed to register the system event which related to screenlock, because: ${err.message}`) 167} 168 ``` 169 170三方应用向锁屏管理服务发起解锁屏幕请求 171 172 ```js 173 174// 三方应用callback方式调用请求解锁 175screenLock.unlockScreen((err) => { 176 if (err) { 177 console.error(`Failed to unlock the screen, because: ${err.message}`); 178 return; 179 } 180 console.info('unlock the screen success successfully.'); 181}); 182 183screenLock.unlock((err, data) => { 184 if (err) { 185 console.error(`Failed to unlock the screen, because: ${err.message}`); 186 return; 187 } 188 console.info(`unlock the screen success successfully. result: ${data}`); 189}); 190 191// 三方应用Promise方式调用请求解锁 192screenLock.unlockScreen().then(() => { 193 console.info('unlock the screen success successfully.'); 194}).catch((err) => { 195 console.error(`Failed to unlock the screen, because: ${err.message}`); 196}); 197 198screenLock.unlock().then((data) => { 199 console.info(`unlock the screen success successfully. result: ${data}`); 200}).catch((err) => { 201 console.error(`Failed to unlock the screen, because: ${err.message}`); 202}); 203 204``` 205 206## 相关仓 207 208**主题框架子系统** 209 210[theme\_screenlock_mgr](https://gitee.com/openharmony/theme_screenlock_mgr) 211 212