HarmonyOS -IntentAgent开发指导

HarmonyOS -IntentAgent开发指导

场景介绍

IntentAgent封装了一个指定行为的Intent,可以通过triggerIntentAgent接口主动触发,也可以与通知绑定被动触发。具体的行为包括:启动Ability和发布公共事件。例如:收到通知后,在点击通知后跳转到一个新的Ability,不点击则不会触发。

接口说明

IntentAgent相关基础类包括IntentAgentHelperIntentAgentInfoIntentAgentConstantTriggerInfo,基础类之间的关系如下图所示:图1 IntentAgent基础类关系图

IntentAgentHelperIntentAgentHelper封装了获取、激发、取消IntentAgent等静态方法。

  • IntentAgentInfoIntentAgentInfo类封装了获取一个IntentAgent实例所需的数据。使用构造函数IntentAgentInfo(int requestCode, OperationType operationType, List<Flags> flags, List<Intent> intents, IntentParams extraInfo)获取IntentAgentInfo对象。
    • requestCode:使用者定义的一个私有值。
    • operationType:为IntentAgentConstant.OperationType枚举中的值。
    • flags:为IntentAgentConstant.Flags枚举中的值。
    • intents:将被执行的意图列表。operationType的值为START_ABILITY,START_SERVICE和SEND_COMMON_EVENT时,intents列表只允许包含一个Intent;operationType的值为START_ABILITIES时,intents列表允许包含多个Intent
    • extraInfo:表明如何启动一个有页面的ability,可以为null,只在operationType的值为START_ABILITY和START_ABILITIES时有意义。
  • IntentAgentConstantIntentAgentConstant类中包含OperationType和Flags两个枚举类:
  • TriggerInfoTriggerInfo类封装了主动激发一个IntentAgent实例所需的数据,使用构造函数TriggerInfo(String permission, IntentParams extraInfo, Intent intent, int code)获取TriggerInfo对象。
    • permission:IntentAgent的接收者的权限名称,只在operationType的值为SEND_COMMON_EVENT时,该参数才有意义。
    • extraInfo:激发IntentAgent时用户自定义的额外数据。
    • intent:额外的Intent。如果IntentAgentInfo成员变量flags包含CONSTANT_FLAG,则忽略该参数;如果flags包含REPLACE_ELEMENT,REPLACE_ACTION,REPLACE_URI,REPLACE_ENTITIES或REPLACE_BUNDLE,则使用额外Intent的element,action,uri,entities或bundleName属性替换原始Intent中对应的属性。如果intent是空,则不替换原始Intent的属性。
    • code:提供给IntentAgent目标的结果码。

开发步骤

获取IntentAgent的代码示例如下:

// 指定要启动的Ability的BundleName和AbilityName字段
// 将Operation对象设置到Intent中
Operation operation = new Intent.OperationBuilder()
        .withDeviceId("")
        .withBundleName("com.testintentagent")
        .withAbilityName("com.testintentagent.entry.IntentAgentAbility")
        .build();
intent.setOperation(operation);
List<Intent> intentList = new ArrayList<>();
intentList.add(intent);
// 定义请求码
int requestCode = 200;
// 设置flags
List<IntentAgentConstant.Flags> flags = new ArrayList<>();
flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);
// 指定启动一个有页面的Ability
IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode, IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);
// 获取IntentAgent实例
IntentAgent agent = IntentAgentHelper.getIntentAgent(this, paramsInfo);

通知中添加IntentAgent的代码示例如下:

int notificationId = 1;
NotificationRequest request = new NotificationRequest(notificationId);
String title = "title";
String text = "There is a normal notification content.";
NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
content.setTitle(title)
       .setText(text);
NotificationContent notificationContent = new NotificationContent(content);
request.setContent(notificationContent); // 设置通知的内容
request.setIntentAgent(agent); // 设置通知的IntentAgent

主动激发IntentAgent的代码示例如下:

int code = 100;
IntentAgentHelper.triggerIntentAgent(this, agent, null, null, new TriggerInfo(null, null, null, code));

相关实例

针对IntentAgent开发指导,有以下示例工程可供参考:

  • IntentAgent本示例演示了如何通过IntentAgent启动Ability和发布公共事件。
0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x