📦 插件介绍
permission_handler_ohos 是 HarmonyCandies 社区推出的 OpenHarmony 权限请求插件,让你在鸿蒙 Flutter 应用中轻松管理和请求运行时权限。
它封装了鸿蒙的权限申请流程,提供简洁的 Dart API,支持检查权限状态、请求单个/多个权限、打开应用设置页面。
| 项目 | 说明 |
|---|---|
| 包名 | permission_handler_ohos |
| 平台 | OpenHarmony / HarmonyOS |
| 仓库 | HarmonyCandies/permission_handler_ohos[1] |
| 开源协议 | MIT |
📥 安装
1. 添加依赖
dependencies:
permission_handler_ohos: any
2. 声明权限
在项目的 module.json5 文件中添加所需权限:
requestPermissions: [
{ name: "ohos.permission.READ_CALENDAR" },
{ name: "ohos.permission.CAMERA" },
{ name: "ohos.permission.MICROPHONE" },
]
⚠️ 鸿蒙的权限分为 system_grant(安装时自动授予)和 user_grant(运行时弹窗申请),请根据官方文档正确配置。
🚀 核心用法
导入
import 'package:permission_handler_ohos/permission_handler_ohos.dart';
检查权限状态
PermissionStatusOhos status =
await PermissionHandlerOhos.checkPermissionStatus(
PermissionOhos.camera.name,
);
请求单个权限
PermissionStatusOhos status =
await PermissionHandlerOhos.requestPermission(
PermissionOhos.camera.name,
);
请求多个权限
Map<String, PermissionStatusOhos> statusMap =
await PermissionHandlerOhos.requestPermissions([
PermissionOhos.camera.name,
PermissionOhos.microphone.name,
PermissionOhos.read_calendar.name,
]);
打开应用设置页面
PermissionHandlerOhos.openAppSettings();
📋 权限状态说明
PermissionStatusOhos 枚举值:
| 值 | 名称 | 含义 |
|---|---|---|
-1 |
denied |
未授权,需在设置中手动开启 |
0 |
granted |
已授权 |
2 |
invalid |
请求无效(未声明权限 / 权限名非法 / 条件不满足) |
🧩 可用权限(部分)
插件内置 PermissionOhos 枚举,涵盖 44 个 OpenHarmony 权限,包括:
internet · camera · microphone · read_calendar · write_calendar · read_contacts · write_contacts · location · approximately_location · location_in_background · read_media_audio · read_media_images · read_media_video · media_location · distributed_data · read_messages · receive_mms · receive_sms · receive_wap_messages · send_messages · read_health_data · activity_recognition · use_bluetooth · discover_bluetooth · get_wifi_info · set_wifi_info · get_network_info · vibrate · nfc_tag · nfc_card_emulation · accelerometer · gyroscope · keep_background_running 等
💡 如果枚举中没有你需要的权限,可以直接传权限的完整字符串名,例如
"ohos.permission.CUSTOM_PERMISSION"。
🔗 相关链接
Pub.dev 包地址[2] GitHub 仓库[3] 鸿蒙权限列表文档[4] 鸿蒙权限管理概述[5]
鸿蒙Flutter插件精选 系列持续更新中!往期回顾:device_info_plus_ohos 等,欢迎关注 🦊
引用链接
[1]HarmonyCandies/permission_handler_ohos: https://github.com/harmonycandies/permission_handler_ohos
[2]Pub.dev 包地址: https://pub-web.flutter-io.cn/packages/permission_handler_ohos
[3]GitHub 仓库: https://github.com/harmonycandies/permission_handler_ohos
[4]鸿蒙权限列表文档: https://gitee.com/openharmony/docs/blob/OpenHarmony-4.1-Release/zh-cn/application-dev/security/AccessToken/permissions-for-all.md
[5]鸿蒙权限管理概述: https://gitee.com/openharmony/docs/blob/OpenHarmony-4.1-Beta1/zh-cn/application-dev/security/AccessToken/app-permission-mgmt-overview.md
夜雨聆风