HarmonyOS 如何实现选择其他APP打开的功能?

如题:HarmonyOS 如何实现选择其他APP打开的功能?

阅读 440
1 个回答

如果应用需要显示在分享面板,则需要构建数据处理能力并按照配置要求在应用配置文件中声明

1,导入相关模块

2,目标应用可实现UIAbility,并从want中获取分享数据

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { systemShare } from '@kit.ShareKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class TestUIAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    systemShare.getSharedData(want)
      .then((data: systemShare.SharedData) => {
        data.getRecords().forEach((record: systemShare.SharedRecord) => {
          // 处理分享数据
        });
      })
      .catch((error: BusinessError) => {
        this.context.terminateSelf();
      });
  }
  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    windowStage.loadContent('pages/Index', (err) => {
      if (err.code) {
        console.error('Failed to load the content. Cause: ' + JSON.stringify(err) ?? '');
        return;
      }
      console.info('Succeeded in loading the content.');
    });
  }
}

3,被分享应用在应用配置文件(src/main/module.json5)配置actrions为ohos.want.action.sendData;uris需穷举所有支持的数据类型。

可以参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-uniformtypedescriptor-V5\#uniformdatatype

"abilities": [
{
  "name": "TestUIAbility",
"srcEntry": "./ets/entryability/TestUIAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
  {
    "actions": [
    "ohos.want.action.sendData"
    ],
    // 目标应用在配置支持接收的数据类型时,需穷举支持的UTD,比如:支持全部图片类型,可声明:general.image,特例:目标应用可以处理全部类型。
    // maxFileSupported 支持的最大数量 不填默认为0
    "uris": [
    {
      "scheme": "file",
    "utd": "general.text",
    "maxFileSupported": 1
    },
    {
      "scheme": "file",
    "utd": "general.png",
    "maxFileSupported": 1
    },
    {
      "scheme": "file",
    "utd": "general.jpeg",
    "maxFileSupported": 1
    }
    ]
  }
  ]
}
]
logo
HarmonyOS
子站问答
访问
宣传栏