立即注册
查看: 415|回复: 0

[鸿蒙OS技术分享] HarmonyOS Ability开发框架(七):IntentAgent开发指导

已绑定手机
发表于 2021-9-18 11:32:18 | 显示全部楼层 |阅读模式 来自 广东省深圳市
场景介绍
IntentAgent封装了一个指定行为的Intent,可以通过triggerIntentAgent接口主动触发,也可以与通知绑定被动触发。具体的行为包括:启动Ability和发布公共事件。例如:收到通知后,在点击通知后跳转到一个新的Ability,不点击则不会触发。

接口说明
IntentAgent相关基础类包括IntentAgentHelper、IntentAgentInfo、IntentAgentConstant和TriggerInfo,基础类之间的关系如下图所示:
图1 IntentAgent基础类关系图
1.jpg

1、IntentAgentHelper
IntentAgentHelper封装了获取、激发、取消IntentAgent等静态方法。
IntentAgentHelper主要接口:
getIntentAgent(Context context, IntentAgentInfo paramsInfo):获取一个IntentAgent实例。
triggerIntentAgent(Context context, IntentAgent agent, IntentAgent.Oncompleted onCompleted, EventHandler handler, TriggerInfo paramsInfo):主动激发一个IntentAgent实例。
cancel(IntentAgent agent):取消一个IntentAgent实例。
judgeEquality(IntentAgent agent, IntentAgent otherAgent):判断两个IntentAgent实例是否相等。
getHashCode(IntentAgent agent):获取一个IntentAgent实例的哈希码。
getBundleName(IntentAgent agent):获取一个IntentAgent实例的包名。
getUid(IntentAgent agent):获取一个IntentAgent实例的用户ID。

2、IntentAgentInfo
IntentAgentInfo类封装了获取一个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时有意义。

3、TriggerInfo
TriggerInfo类封装了主动激发一个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目标的结果码。

4、IntentAgentConstant
IntentAgentConstant类中包含OperationType和Flags两个枚举类:
1.IntentAgentConstant.OperationType
-UNKNOWN_TYPE:不识别的类型。
-START_ABILITY:开启一个有页面的Ability。
-START_ABILITIES:开启多个有页面的Ability。
-START_SERVICE:开启一个无页面的ability。
-SEND_COMMON_EVENT:发送一个公共事件。
2.IntentAgentConstant.Flags
-ONE_TIME_FLAG:IntentAgent仅能使用一次。只在operationType的值为START_ABILITY,START_SERVICE和SEND_COMMON_EVENT时有意义。
-NO_BUILD_FLAG:如果描述IntentAgent对象不存在,则不创建它,直接返回null。只在operationType的值为START_ABILITY,START_SERVICE和SEND_COMMON_EVENT时有意义。
-CANCEL_PRESENT_FLAG:在生成一个新的IntentAgent对象前取消已存在的一个IntentAgent对象。只在operationType的值为START_ABILITY,START_SERVICE和SEND_COMMON_EVENT时有意义。
-UPDATE_PRESENT_FLAG:使用新的IntentAgent的额外数据替换已存在的IntentAgent中的额外数据。只在operationType的值为START_ABILITY,START_SERVICE和SEND_COMMON_EVENT时有意义。
-CONSTANT_FLAG:IntentAgent是不可变的。
-REPLACE_ELEMENT:当前Intent中的element属性可被IntentAgentHelper.triggerIntentAgent()中Intent的element属性取代。
-REPLACE_ACTION: 当前Intent中的action属性可被IntentAgentHelper.triggerIntentAgent()中Intent的action属性取代。
-REPLACE_URI:当前Intent中的uri属性可被IntentAgentHelper.triggerIntentAgent()中Intent的uri属性取代。
-REPLACE_ENTITIES:当前Intent中的entities属性可被IntentAgentHelper.triggerIntentAgent()中Intent的entities属性取代。
-REPLACE_BUNDLE:当前Intent中的bundleName属性可被IntentAgentHelper.triggerIntentAgent()中Intent的bundleName属性取代。

开发步骤
获取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));
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

合作/建议

TEL: 19168984579

工作时间:
周一到周五 9:00-11:30 13:30-19:30
  • 扫一扫关注公众号
  • 扫一扫打开小程序
Copyright © 2013-2024 一牛网 版权所有 All Rights Reserved. 帮助中心|隐私声明|联系我们|手机版|粤ICP备13053961号|营业执照|EDI证
在本版发帖搜索
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表