HarmonyOS鸿蒙Next中想通过卡片打开鸿蒙5自带clock

HarmonyOS鸿蒙Next中想通过卡片打开鸿蒙5自带clock

// WidgetCard_l3_0.ets

@Entry @Component struct widgetCard_l3_0 { // 想通过卡片打开鸿蒙5自带clock //遇到的问题是,使用真机无线调试,bundleName+abilityName都正确,能从hdc打开设置和clock //但是从卡片通过点击按钮的方式却打不开

openClock = () => { try { // 使用系统 URI 方案 postCardAction(this, { //测试打开"设置"无反应 /* action: “router”, bundleName: “com.huawei.hmos.settings”, abilityName: “com.huawei.hmos.settings.MainAbility” */

    /*测试打开"闹钟"无反应
    action: "ohos.want.action.viewData",
    entities: ["entity.system.application"],
    uri: "clock://",
    parameters: {
      source: "widget"
    }

*/ /不写abilityName,点击按钮显示暂无可用打开方式,写了abilityName,点击按钮无反应/ action: “router”, //bundleName: “com.huawei.hmos.clock”, //abilityName: “com.huawei.hmos.clock.phone”, uri: “ability://com.huawei.hmos.clock/com.huawei.hmos.clock.phone?context=widget”, abilityName: “com.huawei.hmos.clock” }); } catch (err) { console.error(“打开时钟失败:” + JSON.stringify(err)); } }

build() { Column() { Button(“打开时钟”) .onClick(this.openClock) .margin(10) .width(180) .height(45) .backgroundColor(’#0A59F7’) .fontColor(Color.White) .fontSize(16) } .width(‘100%’) .height(‘100%’) .justifyContent(FlexAlign.Center) .backgroundColor(’#F1F3F5’) } }


更多关于HarmonyOS鸿蒙Next中想通过卡片打开鸿蒙5自带clock的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

应该是没有给卡片这样的权限,最好是重新设计交互,点击的时候, 拉起app,进行打开,或者拉起app以后,进入一个专用页面进行操作

更多关于HarmonyOS鸿蒙Next中想通过卡片打开鸿蒙5自带clock的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,要通过卡片打开系统自带的时钟应用,可以使用FormExtensionAbility和wantAgent能力。具体步骤如下:

  1. 创建FormExtensionAbility扩展类
  2. 在卡片配置文件中声明formConfig配置
  3. 使用wantAgent设置点击事件:
let wantAgentInfo = {
    wants: [
        {
            bundleName: "com.huawei.clock",
            abilityName: "com.huawei.clock.MainAbility"
        }
    ]
};
wantAgent.getWantAgent(wantAgentInfo).then((agent) => {
    formProvider.setFormNextRefreshTime(formId, 5).then(() => {
        formProvider.updateForm(formId, formBindingData).catch((error) => {});
    });
});

在HarmonyOS Next中通过卡片打开系统时钟应用,需要使用正确的Want参数配置。

  1. 使用系统预定义的时钟应用URI:
openClock = () => {
  try {
    postCardAction(this, {
      action: "ohos.want.action.viewData",
      uri: "clock://",
      parameters: {
        source: "widget"
      }
    });
  } catch (err) {
    console.error("打开时钟失败:" + JSON.stringify(err));
  }
}
  1. 如果上述方式无效,可以尝试使用完整的ability路径:
openClock = () => {
  try {
    postCardAction(this, {
      action: "router",
      bundleName: "com.huawei.hmos.clock",
      abilityName: "com.huawei.hmos.clock.MainAbility"
    });
  } catch (err) {
    console.error("打开时钟失败:" + JSON.stringify(err));
  }
}

注意:

  • 确保应用已声明ohos.permission.START_ABILITIES_FROM_BACKGROUND权限
  • 系统应用的abilityName可能会随版本变化,建议通过hdc shell命令查询当前设备上的实际abilityName,
回到顶部