六华为鸿蒙HarmonyOS应用开发之Java开发模式下的不同Page 间实现页面跳转
Posted 皓月盈江
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了六华为鸿蒙HarmonyOS应用开发之Java开发模式下的不同Page 间实现页面跳转相关的知识,希望对你有一定的参考价值。
不同Page 间导航,实现页面跳转
AbilitySlice作为Page的内部单元,以Action的形式对外暴露,因此可以通过配置Intent的Action导航到目标AbilitySlice。Page间的导航可以使用startAbility()或startAbilityForResult()方法,获得返回结果的回调为onAbilityResult()。在Ability中调用setResult()可以设置返回结果。详细用法可参考根据Operation的其他属性启动应用中的示例。
根据Ability的全称启动应用
通过构造包含BundleName与AbilityName的Operation对象,可以启动一个Ability、并导航到该Ability。示例代码如下:
Intent intent = new Intent();
// 通过Intent中的OperationBuilder类构造operation对象,指定设备标识(空串表示当前设备)、应用包名、Ability名称
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.demoapp")
.withAbilityName("com.demoapp.FooAbility")
.build();
// 把operation设置到intent中
intent.setOperation(operation);
startAbility(intent);
作为处理请求的对象,会在相应的回调方法中接收请求方传递的Intent对象。以导航到另一个Ability为例,导航的目标Ability可以在其onStart()回调的参数中获得Intent对象。
根据Operation的其他属性启动应用
有些场景下,开发者需要在应用中使用其他应用提供的某种能力,而不感知提供该能力的具体是哪一个应用。例如开发者需要通过浏览器打开一个链接,而不关心用户最终选择哪一个浏览器应用,则可以通过Operation的其他属性(除BundleName与AbilityName之外的属性)描述需要的能力。如果设备上存在多个应用提供同种能力,系统则弹出候选列表,由用户选择由哪个应用处理请求。以下示例展示使用Intent跨Ability查询天气信息。
请求方
在Ability中构造Intent以及包含Action的Operation对象,并调用startAbilityForResult()方法发起请求。然后重写onAbilityResult()回调方法,对请求结果进行处理。
private void queryWeather() {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withAction(Intent.ACTION_QUERY_WEATHER)
.build();
intent.setOperation(operation);
startAbilityForResult(intent, REQ_CODE_QUERY_WEATHER);
}
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
switch (requestCode) {
case REQ_CODE_QUERY_WEATHER:
// Do something with result.
...
return;
default:
...
}
}
处理方
1.作为处理请求的对象,首先需要在配置文件中声明对外提供的能力,以便系统据此找到自身并作为候选的请求处理者。
{
"module": {
...
"abilities": [
{
...
"skills":[
{
"actions":[
"ability.intent.QUERY_WEATHER"
]
}
]
...
}
]
...
}
...
}
2.在Ability中配置路由以便支持以此action导航到对应的AbilitySlice。
@Override
protected void onStart(Intent intent) {
...
addActionRoute(Intent.ACTION_QUERY_WEATHER, DemoSlice.class.getName());
...
}
3.在Ability中处理请求,并调用setResult()方法暂存返回结果。
@Override
protected void onActive() {
...
Intent resultIntent = new Intent();
setResult(0, resultIntent); //0为当前Ability销毁后返回的resultCode。
...
}
//==========================================
实例测试:
1.首先创建Empty Ability(Java)的AbilityJump工程,运行出现如下界面:
2.在MainAbility.java同级别添加NewAbility.java文件,在slice文件夹下添加NewAbilitySlice.java文件,在layout文件夹下添加ability_new.xml文件。
NewAbility.java
package com.example.abilityjump;
import com.example.abilityjump.slice.NewAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class NewAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(NewAbilitySlice.class.getName());
}
}
NewAbilitySlice.java
package com.example.abilityjump.slice;
import com.example.abilityjump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
public class NewAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_new);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
ability_new.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_new"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:mainability_new"
ohos:text_size="40vp"
/>
</DirectionalLayout>
3.在中文string.json添加
{
“name”: “mainability_new”,
“value”: “目标测试”
}
string.json
{
"string": [
{
"name": "entry_MainAbility",
"value": "entry_MainAbility"
},
{
"name": "mainability_description",
"value": "Java_Empty Ability"
},
{
"name": "mainability_HelloWorld",
"value": "你好,世界"
},
{
"name": "mainability_new",
"value": "目标测试"
}
]
}
4.在config.json中"abilities"注册NewAbility
config.json
{
"app": {
"bundleName": "com.example.abilityjump",
"vendor": "example",
"version": {
"code": 1000000,
"name": "1.0.0"
}
},
"deviceConfig": {},
"module": {
"package": "com.example.abilityjump",
"name": ".MyApplication",
"mainAbility": "com.example.abilityjump.MainAbility",
"deviceType": [
"phone"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "entry",
"moduleType": "entry",
"installationFree": true
},
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"orientation": "unspecified",
"name": "com.example.abilityjump.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "$string:entry_MainAbility",
"type": "page",
"launchType": "standard"
},
{
"skills": [
{
"actions": [
"action.new"
]
}
],
"orientation": "unspecified",
"name": "com.example.abilityjump.NewAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "$string:entry_MainAbility",
"type": "page",
"launchType": "standard"
}
]
}
}
5.此时编译运行,应该没有错误,还是出现下面效果:
6.设置文本点击事件导航到另外一个Ability页面
参数发送端
MainAbilitySlice.java
package com.example.abilityjump.slice;
import com.example.abilityjump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Text;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//获取文本对象
Text text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
//设置文本的点击事件
text.setClickedListener(component -> {
Intent intent1 = new Intent();
intent1.setParam("data","新页面");
Operation operation = new Intent.OperationBuilder()
.withAction("action.new")
.build();
intent1.setOperation(operation);
startAbility(intent1);
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
参数接收端
NewAbilitySlice.java
package com.example.abilityjump.slice;
import com.example.abilityjump.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
public class NewAbilitySlice extends AbilitySlice {
Text text;
String oldString;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_new);
//获取文本对象
Text text = (Text)findComponentById(ResourceTable.Id_text_new);
oldString = text.getText();
//设置新文本
text.setText(oldString + "," + intent.getStringParam("data"));
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
7.运行程序,效果如下:
欢迎关注公众号,方便技术交流
以上是关于六华为鸿蒙HarmonyOS应用开发之Java开发模式下的不同Page 间实现页面跳转的主要内容,如果未能解决你的问题,请参考以下文章
八华为鸿蒙HarmonyOS应用开发之Java UI框架常用TextField组件使用
七华为鸿蒙HarmonyOS应用开发之Java UI框架常用Text组件和Button组件使用
四华为鸿蒙HarmonyOS应用开发之Java开发下Page Ability生命周期