zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【HarmonyOS】【JAVA UI】鸿蒙点击一个通知,怎么把通知内的文本传递给打开的Ability

JAVA鸿蒙UI 一个 怎么 打开 文本 点击
2023-09-11 14:17:16 时间

​参考资料

【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);
    }

运行效果

8309d2b2d32424ddafb6acb80e993d98_739x985.gif%40900-0-90-f.gif

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh