鸿蒙平台使用 `wakelock_plus` 插件指南

插件介绍

wakelock_plus 是一个用于控制设备唤醒锁的 Flutter 插件,支持在鸿蒙平台上使用。本项目基于 wakelock_plus 开发,提供了简单易用的 API 来启用或禁用设备的唤醒锁,防止设备进入休眠状态。

该插件适用于需要保持设备屏幕常亮的应用场景,如视频播放、阅读、导航等。

安装与使用

安装方式

在引用的项目中,pubspec.yamldependencies 新增配置:

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 插件提供了一种简单高效的方式来控制设备唤醒锁,适用于各种需要保持屏幕常亮的应用场景。该插件具有以下优点:

  1. 简单易用:提供了简洁的 API,只需调用 enable()disable() 方法即可控制唤醒锁。
  2. 跨平台支持:基于原始插件开发,支持在鸿蒙平台上使用。
  3. 性能优异:直接调用系统底层 API,控制唤醒锁的效率高。
  4. 稳定可靠:经过多个版本的测试,确保在不同环境下都能正常工作。

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.youkuaiyun.com

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值