概述
Ability是应用所具备能力的抽象,也是应用程序的重要组成部分。一个应用可以具备多种能力(即可以包含多个Ability),HarmonyOS支持应用以Ability为单位进行部署。
Ability分类:
FA(Feature Ability)
PA(Particle Ability)
每种类型为开发者提供了不同的模板,以便实现不同的业务功能。
Feature Ability(FA)
FA支持Page Ability,简单理解就是手机应用中的一个界面。一个界面就是一个FA。
Page模板是FA唯一支持的模板,用于提供与用户交互的能力。
一个Page实例可以包含一组相关页面,每个页面用一个AbilitySlice实例表示。
Particle Ability(PA)
FA是有有界面的,而Particle Ability(PA)是无界面的,简单理解就是可以运行在后台的一个服务。
PA支持Service Ability和Data Ability:
1)Service模板
用于提供后台运行任务的能力。
2)Data模板
用于对外部提供统一的数据访问抽象。一般是跟数据相关的,比如:如果我们要运行访问数据库。那么就可以用 PA中的Data模板。
Intent
概述:
Intent是对象之间传递信息的载体。
例如,当一个Ability需要启动另一个Ability时,或者一个AbilitySlice需要导航到另一个AbilitySlice时,可以通过Intent指定启动的目标同时携带相关数据。Intent的构成元素包括Operation与Parameters:
Operation
表示要跳转的目的地
Parameters
跳转时所携带的数据
详解:
代码:
ability_main.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">
<Button
ohos:id="$+id:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="无参无返回的跳转"
ohos:text_size="40vp"
ohos:top_margin="30vp"/>
<Button
ohos:id="$+id:but2"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="有参无返回的跳转"
ohos:text_size="40vp"
ohos:top_margin="30vp"/>
<Button
ohos:id="$+id:but3"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="有参有返回的跳转"
ohos:text_size="40vp"
ohos:top_margin="30vp"/>
</DirectionalLayout>
abilityslice1.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:orientation="vertical">
<Text
ohos:id="$+id:text1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="无参无返回的跳转"
ohos:text_size="40vp"/>
</DirectionalLayout>
abilityslice2.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:orientation="vertical">
<Text
ohos:id="$+id:text2"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="有参无返回的跳转"
ohos:text_size="40vp"/>
</DirectionalLayout>
abilityslice3.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:orientation="vertical">
<Text
ohos:id="$+id:text3"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="有参有返回的跳转"
ohos:text_size="40vp"/>
</DirectionalLayout>
MainAbilitySlice.java
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Button but1;
Button but2;
Button but3;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
but1 = (Button) findComponentById(ResourceTable.Id_but1);
but2 = (Button) findComponentById(ResourceTable.Id_but2);
but3 = (Button) findComponentById(ResourceTable.Id_but3);
//绑定事件
but1.setClickedListener(this);
but2.setClickedListener(this);
but3.setClickedListener(this);
}
@Override
public void onClick(Component component) {
if (component == but1){
//无参无返回的跳转
//意图:指在跳转的时候想要做的事情
//1 确定要跳转到哪个设备的ability中(operation)
//2 在跳转的时候是否需要带上参数(parameter)
//如果要在一个ability中进行跳转,那么第一个参数可以不写
//如果在跳转的时候需要带上参数,就写第二个parameter,
// 如果不需要带参数,也可以不写parameter
//创建意图的对象
Intent intent = new Intent();
//跳转
present(new MyAbilitySlice1(),intent);
}else if (component == but2){
//有参无返回的跳转
//创建意图的对象
Intent intent = new Intent();
intent.setParam("username","zhagnsan");
intent.setParam("password","123456");
//跳转
present(new MyAbilitySlice2(),intent);
}else if(component == but3){
//有参有返回的跳转
//创建意图的对象
Intent intent = new Intent();
intent.setParam("username","zhagnsan");
intent.setParam("password","123456");
//跳转
presentForResult(new MyAbilitySlice3(),intent,200);
}
}
//参数1:请求码
//参数2:返回意图对象
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
if (requestCode == 200){
boolean result = resultIntent.getBooleanParam("result", false);
//吐司
ToastDialog td = new ToastDialog(this);
td.setText(result + "");
td.show();
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
MyAbilitySlice1.java
public class MyAbilitySlice1 extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_abilityslice1);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
MyAbilitySlice2.java
public class MyAbilitySlice1 extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_abilityslice1);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
MyAbilitySlice3.java
public class MyAbilitySlice3 extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_abilityslice3);
Text text3 = (Text) findComponentById(ResourceTable.Id_text3);
//获取跳转时传递的用户名
String username = intent.getStringParam("username");
text3.setText(username);
//当点击了文本框时,将数据进行返回
text3.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//创建意图对象,把结果返回
Intent i = new Intent();
i.setParam("result",true);
//返回意图
setResult(i);
//关闭当前子页面
terminate();
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
MainAbility.java
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
//添加主要的路由(子界面)
super.setMainRoute(MainAbilitySlice.class.getName());
//添加其他的主界面
super.addActionRoute("slice1", MyAbilitySlice1.class.getName());
super.addActionRoute("slice2", MyAbilitySlice2.class.getName());
super.addActionRoute("slice3", MyAbilitySlice3.class.getName());
}
}
不同页面中的跳转
- 同一页面中,不同子页面的跳转
- A页面跳转B页面
- A页面跳转B页面中其他子界面
代码:
ability_main.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">
<Button
ohos:id="$+id:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#21A8FD"
ohos:layout_alignment="horizontal_center"
ohos:text="跳转1"
ohos:text_size="40vp"/>
<Button
ohos:id="$+id:but2"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="#791651"
ohos:layout_alignment="horizontal_center"
ohos:text="跳转2"
ohos:text_size="40vp"
ohos:top_margin="30vp"/>
</DirectionalLayout>
ability_second.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_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_second"
ohos:layout_alignment="horizontal_center"
ohos:text="第二个页面中的主要子页面"
ohos:text_size="40vp"
/>
</DirectionalLayout>
ability_second_nomain.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_helloworld"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_second"
ohos:layout_alignment="horizontal_center"
ohos:text="第二个页面中其他子页面"
ohos:text_size="40vp"
/>
</DirectionalLayout>
MainAbilitySlice.java
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Button but1;
Button but2;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
but1 = (Button) findComponentById(ResourceTable.Id_but1);
but2 = (Button) findComponentById(ResourceTable.Id_but2);
but1.setClickedListener(this);
but2.setClickedListener(this);
}
@Override
public void onClick(Component component) {
if(component == but1){
//跳转到第二个页面中的主要子界面
Intent i = new Intent();
Operation build = new Intent.OperationBuilder()
.withDeviceId("")//本机
.withBundleName("com.example.myapplication")//应用
.withAbilityName("com.example.myapplication.SecondAbility")
.build();
i.setOperation(build);
startAbility(i);
}else if (component == but2){
//跳转到第二个页面中的其他子界面
Intent i = new Intent();
Operation build = new Intent.OperationBuilder()
.withAction("secondabilityslicenomain")
.build();
i.setOperation(build);
startAbility(i);
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
SecondAbilitySlice.java
public class SecondAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_second);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
SecondAbilitySliceNoMain.java
public class SecondAbilitySliceNoMain extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_second_nomain);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
SecondAbility.java
public class SecondAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(SecondAbilitySlice.class.getName());
addActionRoute("secondabilityslicenomain", SecondAbilitySliceNoMain.class.getName());
}
}