编程语言
首页 > 编程语言> > 【HarmonyOS】【JAVA UI】鸿蒙点击一个通知,怎么把通知内的文本传递给打开的Ability

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

运行效果

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

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

标签:JAVA,Ability,通知,intent,import,new,super,public,Intent
来源: https://www.cnblogs.com/developer-huawei/p/16553215.html