ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-08-05 09:32:54  阅读:205  来源: 互联网

标签:JAVA Ability 通知 intent import new super public Intent


​参考资料

【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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有