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

接口说明
IntentAgent 相关基础类包括 IntentAgentHelper、 IntentAgentInfo、 IntentAgentConstant 和 TriggerInfo,基础类之间的关系如下图所示:
图1 IntentAgent 基础类关系图
IntentAgentHelper封装了获取、激发、取消IntentAgent等静态方法。
接口名 | 描述 |
---|---|
getIntentAgent(Contextcontext,IntentAgentInfoparamsInfo) | 获取一个IntentAgent实例。 |
triggerIntentAgent(Contextcontext,IntentAgentagent,IntentAgent.OncompletedonCompleted,EventHandlerhandler,TriggerInfoparamsInfo) | 主动激发一个IntentAgent实例。 |
cancel(IntentAgentagent) | 取消一个IntentAgent实例。 |
judgeEquality(IntentAgentagent,IntentAgentotherAgent) | 判断两个IntentAgent实例是否相等。 |
getHashCode(IntentAgentagent) | 获取一个IntentAgent实例的哈希码。 |
getBundleName(IntentAgentagent) | 获取一个IntentAgent实例的包名。 |
getUid(IntentAgentagent) | 获取一个IntentAgent实例的用户ID。 |
IntentAgentInfo 类封装了获取一个 IntentAgent 实例所需的数据。使用构造函数 IntentAgentInfo(int requestCode, OperationType operationType, List
IntentAgentConstant 类中包含 OperationType 和 Flags 两个枚举类:
类名 | 枚举值 |
---|---|
IntentAgentConstant.OperationType | UNKNOWN_TYPE:不识别的类型。START_ABILITY:开启一个有页面的Ability。START_ABILITIES:开启多个有页面的Ability。START_SERVICE:开启一个无页面的ability。SEND_COMMON_EVENT:发送一个公共事件。 |
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属性取代。 |
TriggerInfo 类封装了主动激发一个 IntentAgent 实例所需的数据,使用构造函数TriggerInfo( String permission, IntentParams extraInfo, Intent intent, int code) 获取 TriggerInfo 对象。
开发步骤
获取 IntentAgent 的代码示例如下:
// 指定要启动的Ability的BundleName和AbilityName字段// 将Operation对象设置到Intent中Operation operation = new Intent.OperationBuilder().withDeviceId("").withBundleName("com.huawei.testintentagent").withAbilityName("com.huawei.testintentagent.entry.IntentAgentAbility").build();intent.setOperation(operation);List intentList = new ArrayList<>();intentList.add(intent);// 定义请求码int requestCode = 200;// 设置flagsList flags = new ArrayList<>();flags.add(Flags.UPDATE_PRESENT_FLAG);// 指定启动一个有页面的AbilityIntentAgentInfo 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.";NotificationNormalContent content = new 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 ));
three little pigs 三只小猪 storyteller: there are three little pigs living with 讲故事者:三只小猪和他们的妈妈 their mother. ding-ding and dong- 住在一起。 丁丁和东东 dong are brother pigs. they are very 是猪哥哥,他们很懒,他 lazy. they eat and sleep all day. 们整天吃了就睡。 龙龙是 long-long is the youngest pig. she 最小的,她整天帮着妈妈 works all day. she helps her mother 做家务。 to do the housework. mother pig: you have grown up. you must make 猪妈妈:你们已经长大了,你们得 your own houses. 为自己盖间房。 goodbye, little pigs. build a house. 再见,孩子们。 去盖间房。 be careful of the wolf. 小心狼。 pigs: yes, mum. goodbye. 三只小猪:好的,妈妈。 再见。 wolf: i’m hungry. look! three little pigs 狼:我饿了。 看,三只小猪可 for dinner. mmm. 以做我的美餐。 sister pig: what are you doing, brothers? 猪妹妹:哥哥,你们在干什么? ding-ding: i’m building a house with leaves. 丁丁:我在用树叶盖房子。 dong-dong: i’m building a house with sticks. 东东:我在用树枝盖房子。 sister pig: but leaves aren’t strong. 猪妹妹:可是树叶不牢固。 树枝也不 and sticks aren’t strong. 牢固。 brother pigs: yes, we know. but it’s easy. 猪哥哥:我们知道。 但它很容易。 what are you doing, sister? 你在干什么? sister pig: i’m building a house with bricks. 猪妹妹:我在用砖头盖房子。 brother pigs: bricks! that’s very difficult. 猪哥哥:用砖头盖,那太难了。 sister pig: i know. but bricks are strong. 猪妹妹:我知道,可是砖头很坚固。 …… brother pigs: oh, we’re finished. let’s have 猪哥哥:噢,我们盖好了。 我们打 a nap. 个盹吧。 sister pig: my house is finished. my house 猪妹妹:我的房子盖好了,我的房 is strong. 很坚固。 wolf: little pigs. little pigs. open your 狼: 小猪,小猪,快开门! doors! brother pigs: no. no. go away. 猪哥哥:不开,不开。 快走开。 wolf: this is very easy. sticks and leaves 狼:这太容易了。 树枝和树叶 aren’t strong. 都不牢固。 storyteller: the wolf blows the houses down. 讲故事者:狼吹倒了房屋。 丁丁和东 ding-ding and dong-dong run 东跑到龙龙家。 to long-long’s house. brother pigs: help! help! 猪哥哥:救命! 救命! wolf: stop, you two pigs. 狼: 站住,你这两只小猪。 brother pigs: open the door, sister. the wolf 猪哥哥:妹妹,快开门。 狼来了, is coming. let me in. 让我们进去。 long-long: come in, please. don’t worry. 龙龙:快进来。 别担心,我的 my house is strong. 房子很坚固。 wolf: where did they go? oh they are 狼:他们跑哪儿了?噢,他们 here. little pigs. little pigs. open 在这儿。 小猪,快把门打 the door! 开。 three pigs: no. no. go away. you bad wolf. 三只小猪:不开,不开。 快走开。 你这只恶狼。 wolf: i’ll blow it down. 狼:我要吹倒它。 storyteller: the wolf blows and blows. he 讲故事者:狼吹呀吹,它不停地吹。 blows and blows. but the house 可是房子非常坚固。 is very strong. wolf: i’ll hit it. oh. 狼:我要撞倒它。 噢! long-long: let’s boil the water. 龙龙:我们把水烧开。 brother pigs: ok. 猪哥哥:好的。 wolf: oh, oh! it’s hot. it’s hot. 狼:噢!噢!好烫,好烫。 three pigs: hooray! the wolf is dead. the 三只小猪:好啊!狼死了,狼死了。 wolf is dead storyteller: since then, ding-ding and dong- 讲故事者:从那以后,丁丁、东东 dong work hard with long-long. 和龙龙一起努力工作。 they work and play together. 他们一起工作,一起玩。
发表评论