参考资料
代码运行
准备工作
新建一个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);
}
运行效果
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/279021.html