华为Harmony鸿蒙开发笔记九:IntentAgent
直观的来看,IntentAgent就是写一个Intent,设置好,但不执行,需要执行的时候再触发。
private IntentAgent agent; private void initAgent() { Intent intent = new Intent(); // 指定要启动的Ability的BundleName和AbilityName字段 // 将Operation对象设置到Intent中 Operation operation = new Intent.OperationBuilder() .withDeviceId("") .withBundleName("com.example.intentagentdemo") .withAbilityName("com.example.intentagentdemo.SecondAbility") .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实例 agent = IntentAgentHelper.getIntentAgent(this, paramsInfo); }
然后是触发
@Override public void onClick(Component component) { switch (component.getId()) { case ResourceTable.Id_btn_notification_publish: int code = 100; IntentAgentHelper.triggerIntentAgent(MainAbilitySlice.this, agent, null, null, new TriggerInfo(null, null, null, code)); break; default: break; } }
其实这里,可以直接调用startAbility()方法,用IntentAgent绕了一大圈,感觉像发明了一种蟑螂药,只要抓住蟑螂,涂在嘴上,蟑螂必死一样。
上一篇:
通过多线程提高代码的执行效率例子
下一篇:
docker具名挂载与匿名挂载