更新时间:2022-05-01 10:54:09浏览次数:148+次
以下介绍HarmonyOS JAVA UI技术之鸿蒙点击一个通知,如何把通知内的文本传递给打开的Ability?本文参考了鸿蒙HarmonyOS JAVA UI自定义通知的实现,附代码,及鸿蒙官方的IntentAgent开发指导文档。
代码运行
准备工作及相关代码:
新建一个TwoAbility,在TwoAbilitySlice中使用Intent去接受参数,代码如下,获取的关键字是“key”:
package com.harmony.alliance.mynotification.slice;
import com.harmony.alliance.mynotification.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
public class TwoAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_two);
Intent my=getAbility().getIntent();
String result= (String) my.getParams().getParam("key");
Text text= findComponentById(ResourceTable.Id_text_helloworld);
text.setText(result);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
在通知标题上设置点击事件,代码如下,设置参数关键字为“key”、value为“value”:
private IntentAgent SetIntentAgent() {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withAbilityName(TwoAbility.class.getName())
.withBundleName(getBundleName())
.build();
intent.setOperation(operation);
intent.setParam("key", "value");
List<Intent> list = new ArrayList<>();
list.add(intent);
IntentParams intentParams = new IntentParams();
IntentAgentInfo agentInfo = new IntentAgentInfo(1001, IntentAgentConstant.OperationType.START_ABILITY,
IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG, list, intentParams);
return IntentAgentHelper.getIntentAgent(this, agentInfo);
}
运行效果
以上动画图是真实的效果。
相关资讯