文档开发者中心
我的
|

UI

DevEco Studio

关于使用显示want拉起ability的问题。

使用显示want可以拉起自己的ability,而无法拉起其它应用的ability,错误提示如下:

Device info:OpenHarmony 3.2

Build info:OpenHarmony 3.2.9.1

Module name:com.example.kkkk

Version:1.0.0

Pid:4640

Uid:20040005

Lifetime: 0.000000s

Js-Engine: ark

page: pages/Index.js

Error message: Invalid parameter

请问各位大佬是怎么回事呀?谢谢!

点赞
收藏
回复
2
分享
举报
浏览324 发布于2024-01-09 22:23山西
全部评论
最多点赞
最新发布
最早发布
贴一下源代码
1
1楼回复于2024-01-10 00:41 来自山东
  • 张跃斌
    共有2个文件,一个是zyb.ets ,如下: /* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0h+-/$%-+ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent'; import notification from '@ohos.notificationManager'; import hilog from '@ohos.hilog'; /** * Obtains the WantAgent of an application. * * @returns WantAgent of an application. */ export function createWantAgent(bundleName: string, abilityName: string): Promise<WantAgent> { let wantAgentInfo = { wants: [ { deviceId: '', bundleName: bundleName, abilityName: abilityName, action: '', entities: [], uri: '', parameters: {} } ], operationType: wantAgent.OperationType.START_ABILITY, requestCode: 0, wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG] } as wantAgent.WantAgentInfo; return wantAgent.getWantAgent(wantAgentInfo); } /** * Publish notification. * * @param progress Download progress * @param title Notification title. * @param wantAgentObj The want of application. */ export function publishNotification( wantAgentObj: WantAgent) { let template:notification.NotificationTemplate = { name: 'downloadTemplate', data: { progressValue: 1, progressMaxValue: 100, isProgressIndeterminate: false } }; let notificationRequest: notification.NotificationRequest = { id: 1000, slotType: notification.SlotType.CONTENT_INFORMATION, // Construct a progress bar template. The name field must be set to downloadTemplate. template: template, content: { contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: "test", text: 'test', additionalText: "haha" } }, wantAgent: wantAgentObj }; notification.publish(notificationRequest).catch((err: Error) => { console.log(JSON.stringify(err)); }); } /** * open notification permission */ export function openNotificationPermission() { notification.requestEnableNotification().then(() => { console.log('Enable notification success'); }).catch((err) => { console.log(JSON.stringify(err)); }); } 另一个是index.ets,如下: //import WantAgent from '@ohos.wantAgent'; import common from '@ohos.app.ability.common'; import vibrator from '@ohos.vibrator'; import NotificationManager from '@ohos.notificationManager'; import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent'; import { createWantAgent, publishNotification, openNotificationPermission } from '../zyb'; //import notification from '@ohos.notification'; @Entry @Component struct Index { @State message: string = 'Hello World' private wantAgentObj: WantAgent = new Object(); private context = getContext(this) as common.UIAbilityContext; aboutToAppear(){ } build() { Flex({direction:FlexDirection.Column,justifyContent:FlexAlign.SpaceEvenly}) { Text(this.message) .height('10%') .fontSize(30) .fontWeight(FontWeight.Bold) Button('创建') .width(200) .height('10%') .fontSize(30) .onClick( () => { openNotificationPermission(); //let bname=this.context.abilityInfo.bundleName; //let aname = this.context.abilityInfo.name; let bname='com.example.hhh'; let aname='EntryAbility'; createWantAgent(bname,aname).then(want => { this.wantAgentObj = want; }).catch((err: Error) => { console.log(JSON.stringify(err)); }); }) Button('发布') .width(200) .height('10%') .fontSize(30) .onClick(()=>{ publishNotification(this.wantAgentObj); }) } .padding(50) .height('100%') } }function sleep(arg0: number) { throw new Error('Function not implemented.'); } 麻烦您了,提前谢谢啦。
    展开全部
    2024-01-10 21:14 来自山西
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。