HarmonyOS鸿蒙Next中添加快捷方式checkPinShortcutPermitted函数报错401,参数不对,不理解哪个参数不对
HarmonyOS鸿蒙Next中添加快捷方式checkPinShortcutPermitted函数报错401,参数不对,不理解哪个参数不对
Button("添加快捷方式").onClick(() => {
try {
const uiContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
const shortcutId = `${Date.now()}`;
const want: Want = {
bundleName: "com.czc.theRestSkinHarm",
moduleName: "entry",
abilityName: "EntryAbility",
parameters: {
testKey: "testValue"
}
}
const label = "shortcut";
const foregroundIcon = uiContext.filesDir + "/icon.png";
const backgroundIcon = "";
console.log("foregroundIcon",foregroundIcon)
productViewManager.checkPinShortcutPermitted(uiContext, shortcutId, want, "123",
foregroundIcon, "")
.then((res) => {
console.log("成功", JSON.stringify(res))
}).catch((error: BusinessError) => {
hilog.error(0x0001, 'TAG',
`checkPinShortcutPermitted error. code is ${error.code}, message is ${error.message}`);
})
} catch (e) {
console.log("err", JSON.stringify(e as BusinessError))
}
})
更多关于HarmonyOS鸿蒙Next中添加快捷方式checkPinShortcutPermitted函数报错401,参数不对,不理解哪个参数不对的实战教程也可以访问 https://www.itying.com/category-93-b0.html
可能由以下原因导致:
-
参数顺序问题:检查发现您传入的
label
参数"123"与文档要求的参数顺序不符。正确顺序应该是:uiContext
shortcutId
want
label
foregroundIcon
backgroundIcon
-
label
参数值问题:您传入的"123"可能不符合要求,label
应该是字符串类型的快捷方式显示名称,建议使用有意义的名称如"shortcut"。 -
图标路径问题:
foregroundIcon
路径虽然打印了,但需要确认:- 文件确实存在
- 路径格式正确
- 应用有访问权限
建议修改为:
productViewManager.checkPinShortcutPermitted(
uiContext,
shortcutId,
want,
label, // 使用定义的label变量
foregroundIcon,
backgroundIcon
)
同时检查:
- 确保
icon.png
文件已正确放置在filesDir
目录。 - 确认应用已申请
ohos.permission.PUBLISH_DESKTOP_SHORTCUT
权限。