插件介绍
wakelock_plus 是一个用于控制设备唤醒锁的 Flutter 插件,支持在鸿蒙平台上使用。本项目基于 wakelock_plus 开发,提供了简单易用的 API 来启用或禁用设备的唤醒锁,防止设备进入休眠状态。
该插件适用于需要保持设备屏幕常亮的应用场景,如视频播放、阅读、导航等。
安装与使用
安装方式
在引用的项目中,pubspec.yaml 中 dependencies 新增配置:
dependencies:
wakelock_plus_ohos:
git:
url: "https://atomgit.com/openharmony-sig/fluttertpc_wakelock_plus.git"
path: "wakelock_plus/ohos"
需覆盖 wakelock_plus_platform_interface 版本:
dependency_overrides:
wakelock_plus_platform_interface: 1.1.0
执行命令安装依赖:
flutter pub get
基本使用示例
以下是一个简单的使用示例,展示如何在鸿蒙平台上使用 wakelock_plus 插件控制设备唤醒锁:
import 'package:flutter/material.dart';
import 'package:wakelock_plus_ohos/wakelock_plus_ohos.dart';
void main() {
runApp(const WakelockExampleApp());
}
/// Example app widget demonstrating how to use the wakelock plugin.
///
/// The example implementation is located inside [OutlinedButton.onPressed]
/// callback functions and a [FutureBuilder].
class WakelockExampleApp extends StatefulWidget {
/// Creates the [WakelockExampleApp] widget.
const WakelockExampleApp({Key? key}) : super(key: key);
State<WakelockExampleApp> createState() => _WakelockExampleAppState();
}
class _WakelockExampleAppState extends State<WakelockExampleApp> {
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Wakelock example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
const Spacer(
flex: 3,
),
OutlinedButton(
onPressed: () {
// The following code will enable the wakelock on the device
// using the wakelock plugin.
// You could also use Wakelock.toggle(on: true);
WakelockPlus.enable().whenComplete(() {
setState(() {});
});
},
child: const Text('enable wakelock'),
),
const Spacer(),
OutlinedButton(
onPressed: () {
// The following code will disable the wakelock on the device
// using the wakelock plugin.
// You could also use Wakelock.toggle(on: false);
WakelockPlus.disable().whenComplete(() {
setState(() {});
});
},
child: const Text('disable wakelock'),
),
const Spacer(
flex: 2,
),
FutureBuilder(
future: WakelockPlus.enabled,
builder: (context, AsyncSnapshot<bool> snapshot) {
final data = snapshot.data;
// The use of FutureBuilder is necessary here to await the
// bool value from the `enabled` getter.
if (data == null) {
// The Future is retrieved so fast that you will not be able
// to see any loading indicator.
return Container();
}
return Text('The wakelock is currently '
'${data ? 'enabled' : 'disabled'}.');
},
),
const Spacer(
flex: 3,
),
],
),
),
),
);
}
}
核心 API 说明
| API 名称 | 描述 | 返回值 | 鸿蒙支持 |
|---|---|---|---|
enable | 启用唤醒锁 | Future<void> | yes |
disable | 禁用唤醒锁 | Future<void> | yes |
toggle | 根据布尔值切换唤醒锁状态 | Future<void> | yes |
enabled | 检查当前唤醒锁状态 | Future<bool> | yes |
约束与限制
兼容性
在以下版本中已测试通过:
Flutter: 3.7.12-ohos-1.0.6; SDK: 5.0.0(12); IDE: DevEco Studio: 5.0.13.200; ROM: 5.1.0.120 SP3;
总结
wakelock_plus 插件提供了一种简单高效的方式来控制设备唤醒锁,适用于各种需要保持屏幕常亮的应用场景。该插件具有以下优点:
- 简单易用:提供了简洁的 API,只需调用
enable()或disable()方法即可控制唤醒锁。 - 跨平台支持:基于原始插件开发,支持在鸿蒙平台上使用。
- 性能优异:直接调用系统底层 API,控制唤醒锁的效率高。
- 稳定可靠:经过多个版本的测试,确保在不同环境下都能正常工作。
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.youkuaiyun.com
1204

被折叠的 条评论
为什么被折叠?



