HarmonyOS 鸿蒙Next getWantAgent的使用

HarmonyOS 鸿蒙Next getWantAgent的使用

@Entry @Component export struct AboutUs {

publishNotification() { // 通知发送 let wantAgentObj = null; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。

// 通过WantAgentInfo的operationType设置动作类型。
let wantAgentInfo = {
  wants: [
    {
      deviceId: '',
      bundleName: 'com.example.test',
      abilityName: 'com.example.test.MainAbility',
      action: '',
      entities: [],
      uri: '',
      parameters: {}
    }
  ],
  operationType: wantAgent.OperationType.START_ABILITY,
  requestCode: 0,
  wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
}

// 创建WantAgent
wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
  if (err) {
    console.error('[WantAgent]getWantAgent err=' + JSON.stringify(err));
  } else {
    console.info('[WantAgent]getWantAgent success');
    wantAgentObj = data;
  }
});

// 构造NotificationRequest对象
let notificationRequest = {
  content: {
    contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
    normal: {
      title: 'Test_Title',
      text: 'Test_Text',
      additionalText: 'Test_AdditionalText',
    },
  },
  id: 1,
  label: 'TEST',
  wantAgent: wantAgentObj,
}

Notification.publish(notificationRequest, (err) => {
  if (err) {
    console.error(`[ANS] failed to publish, error[${err}]`);
    return;
  }
  console.info(`[ANS] publish success `);
});

}

build(){ Column(){ Text(“关于我们”)

    Button('发送通知')
      .width('50%')
      .margin({bottom:10})
      .onClick(() => {
        this.publishNotification()
      })
}

}

}

无效参数什么意思 安装官方文档咋不对呢 有没有大佬解释一下


更多关于HarmonyOS 鸿蒙Next getWantAgent的使用的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

运行顺序

let wantAgentObj ; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。
console.info('TAG getWantAgent 外 ' + wantAgentObj)
// 通过WantAgentInfo的operationType设置动作类型。
let wantAgentInfo = {
  wants: [
    {
      deviceId: '',
      bundleName: 'com.example.newmodelui',
      abilityName: 'com.example.newmodelui.EntryAbility',
      action: '',
      entities: [],
      uri: '',
      parameters: {}
    }
  ],
  operationType: wantAgent.OperationType.START_ABILITY,
  requestCode: 0,
  wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
}
try {
  // 创建WantAgent
  wantAgent.getWantAgent(wantAgentInfo).then(data=>{
    wantAgentObj=data
    console.info('TAG getWantAgent 方法内 --- ' + wantAgentObj)
  });
} catch (err){
}

console.info('TAG 外 --- ' + wantAgentObj)

打印结果 :

04-29 15:07:52.772 24912-2275/com.example.newmodelui I 0FEFE/JsApp: TAG getWantAgent 外 undefined

04-29 15:07:52.773 24912-2275/com.example.newmodelui I 0FEFE/JsApp: TAG 外 — undefined

04-29 15:07:52.791 24912-2275/com.example.newmodelui I 0FEFE/JsApp: TAG getWantAgent 方法内 — [object Object]

在mate上还没打印只有在模拟器上才有打印我吐了

更多关于HarmonyOS 鸿蒙Next getWantAgent的使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


引用应该错了,如果要正确,需要用下边这个

```javascript
import Notification from '[@ohos](/user/ohos).notification';

官方最新的Notification的发布,应该用

import NotificationManager from '[@ohos](/user/ohos).notificationManager';

用的是官方的 只是把名字改了,

希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。

Build info: OpenHarmony 3.2.9.1

咋看起来不像是在开发HarmonyOS手机应用?

HarmonyOS手机应用

不知道出现什么问题了

在HarmonyOS(鸿蒙)Next中,getWantAgent是用于创建一个WantAgent对象的方法。WantAgent是鸿蒙系统中用于封装一个意图(Want)和执行该意图的代理对象。WantAgent可以用于启动Ability、发送广播、启动服务等操作。

getWantAgent方法通常通过WantAgentHelper类来调用,其基本使用方式如下:

import wantAgent from '@ohos.app.ability.wantAgent';
import { BusinessError } from '@ohos.base';

let wantAgentObj: wantAgent.WantAgent;
let want: wantAgent.Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

let operationInfo: wantAgent.OperationInfo = {
  wants: [want],
  operationType: wantAgent.OperationType.START_ABILITY,
  requestCode: 0,
  wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};

try {
  wantAgent.getWantAgent(operationInfo, (err: BusinessError, data: wantAgent.WantAgent) => {
    if (err) {
      console.error(`Failed to get WantAgent. Code is ${err.code}, message is ${err.message}`);
      return;
    }
    wantAgentObj = data;
    console.info('Succeeded in getting WantAgent.');
  });
} catch (err) {
  console.error(`Failed to get WantAgent. Code is ${err.code}, message is ${err.message}`);
}

在上述代码中,getWantAgent方法接收一个OperationInfo对象作为参数,该对象包含了意图(Want)、操作类型(OperationType)、请求码(requestCode)以及标志(wantAgentFlags)等信息。getWantAgent方法会返回一个WantAgent对象,可以用于后续的操作。

WantAgent的使用场景包括但不限于:

  1. 启动一个Ability。
  2. 启动一个服务。
  3. 发送一个广播。

WantAgent的创建和使用是鸿蒙系统中实现跨应用交互和任务调度的关键机制之一。

回到顶部