当前位置: 首页 > news >正文

鸿蒙 - 分享功能

文章目录

  • 一、背景
  • 二、app发起分享
    • 1. 通过分享面板进行分享
    • 2. 使用其他应用打开
  • 二、处理分享的内容
    • 1. `module.json5` 配置可接收分享
    • 2. 解析分享的数据

一、背景

在App开发中,分享是常用功能,这里介绍鸿蒙开发中,其他应用分享到自己的app中,或者自己的app分享给其他app
鸿蒙系统分享地址

二、app发起分享

1. 通过分享面板进行分享

  1. 导入相关模块。
import { common } from '@kit.AbilityKit';
import { systemShare } from '@kit.ShareKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
  1. 获取统一数据类型
    可以自己确定分享的类型,也可以调用方法通过后缀获取分享类型
    getUniformDataTypeByFilenameExtension
let utdTypeId = ""if (file.extension.length == 0) {utdTypeId = utd.UniformDataType.FOLDER} else {utdTypeId = utd.getUniformDataTypeByFilenameExtension(file.extension, utd.UniformDataType.OBJECT);}if (utdTypeId.length == 0) {promptAction.showToast({message: appUtils.getResString('share_tip2')})return}
  1. 构造分享数据,可添加多条分享记录。
let shareData: systemShare.SharedData = new systemShare.SharedData({utd: utdTypeId,uri: file.uri});
  1. 启动分享面板时,配置分享面板显示的位置信息或关联的组件ID,面板将以Popup形式展示。
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);// 获取UIAbility上下文对象let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;// 进行分享面板显示controller.show(context, {previewMode: systemShare.SharePreviewMode.DEFAULT,selectionMode: systemShare.SelectionMode.SINGLE});

2. 使用其他应用打开

通过context调用startAbility,弹出系统弹窗,使用其他应用打开文件

// Construct request data Want, taking opening a Word file as an examplelet wantInfo: Want = {uri: file.uri, // Indicate the URI path of the file to be opened, usually used in conjunction with typetype: 'application/msword', // Indicate the type of file to be openedflags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, // Authorization to perform write operations on URI}// Call the startAbility interface to open fileslet context = getContext(this) as common.UIAbilityContext;context.startAbility(wantInfo).then(() => {console.info("分享成功");}).catch((err: BusinessError) => {console.info("分享失败");})

二、处理分享的内容

1. module.json5 配置可接收分享

module.json5 下找到 abilities标签,找到 skills

  1. 配置entities添加 "entity.system.share"
"entities": ["entity.system.home","entity.system.share"],
  1. 配置actions,添加actions
"actions": ["action.system.home","ohos.want.action.select","ohos.want.action.sendData","ohos.want.action.viewData" // 必填,声明数据处理能力],
  1. 配置uris
"uris": [{"scheme": "file",// 物理存储类型的基类型"utd": "general.entity","maxFileSupported": 1,"linkFeature": "FileOpen"},{"scheme": "file",// 逻辑内容类型的基类型"utd": "general.object","maxFileSupported": 1,"linkFeature": "FileOpen"}]

2. 解析分享的数据

  1. 在其onCreate或onNewWant回调中获取传入的want参数
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);// 注入Ability上下文到AppUtilsAppUtils.getInstance().context = this.context;hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');ShareManager.getInstance().handelShareData(want);}
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {ShareManager.getInstance().handelShareData(want)}
  1. 通过getSharedData 解析分享数据
/** 处理分享过来的文件 */public handelShareData(want: Want) {systemShare.getSharedData(want).then((data: systemShare.SharedData) => {data.getRecords().forEach((record: systemShare.SharedRecord) => {// 处理分享数据});}).catch((error: BusinessError) => {DKLogger.error(`Failed to getSharedData. Code: ${error.code}, message: ${error.message}`);// this.context.terminateSelf();if (want.action == 'ohos.want.action.sendData'|| want.action == 'ohos.want.action.viewData') {}});}

处理完分享数据,即可将数据在页面显示

http://www.lryc.cn/news/613512.html

相关文章:

  • MySql MVCC的原理总结
  • 软件加密工具-DSProtector使用说明
  • 2025年华数杯C题超详细解题思路
  • 旅游mcp配置(1)
  • 多场景两阶段分布式鲁棒优化模型、数据驱动的综合能源系统
  • pybind11 的应用
  • C语言feof函数详解:文件末尾检测的实用工具
  • 【华为机试】113. 路径总和 II
  • 计算机网络1-5:计算机网络的性能指标
  • CSS--:root指定变量,其他元素引用
  • [安卓按键精灵开发工具]本地数据库的初步学习
  • 剑指offer第2版——面试题1:赋值运算符函数
  • CPTS Remote 复现
  • react-router/react-router-dom
  • 深度学习中主要库的使用:(一)pandas,读取 excel 文件,支持主流的 .xlsx/.xls 格式
  • 房产证识别在房产行业的技术实现及应用原理
  • 超高车辆如何影响城市立交隧道安全?预警系统如何应对?
  • 网络基础概念
  • 基于Qt的Live2D模型显示以及控制
  • ora-01658 无法为表空间 users中的段创建initial区
  • RocketMQ架构解析
  • 遥感卫星领域的AI应用
  • Day03 学习git
  • LWIP网络接口管理
  • [airplay2] airplay2简略介绍
  • 二分查找算法,并分析其时间、空间复杂度
  • IIS7.5下的https无法绑定主机头,显示灰色如何处理?
  • 前后端加密传数据实现方案
  • [ java SE ] 多人聊天窗口1.0
  • 强光干扰下裂缝漏检率↓82%!陌讯轻量化模型在道路巡检的落地实践