赞
踩
Ability是应用所具备能力的抽象,也是应用程序的重要组成部分。一个应用可以具备多种能力(即可以包含多个Ability),HarmonyOS支持应用以Ability为单位进行部署。Ability可以分为FA(Feature Ability)和PA(Particle Ability)两种类型,每种类型为开发者提供了不同的模板,以便实现不同的业务功能。
FA支持
Page Ability:
Page模板是FA唯一支持的模板,用于提供与用户交互的能力。一个Page实例可以包含一组相关页面,每个页面用一个AbilitySlice实例表示。有点类似Android的Activity组件
PA支持Service Ability和Data Ability:
页面:显示UI的功能。UI通过AbilitySlice呈现。您必须重写onStart(ohos.aafwk.content.Intent)方法,并使用setMainRoute(java.lang.String)和addActionRoute(java.lang.String,java.lang.String)方法来配置Page功能的条目。
服务:一种在后台运行且没有UI的功能。它用于开发始终在后台运行或与其他功能连接的服务。当服务能力与其他能力连接时,将返回一个远程对象,您可以使用该远程对象来调用该服务能力提供的功能。
数据:一种用于操作数据且没有UI的功能。它提供了用于插入,删除,更新和查询数据以及打开文件的方法。您必须实现这些方法。
在配置文件(config.json)中注册Ability时,可以通过配置Ability元素中的“type”属性来指定Ability模板类型,示例如下。
其中,“type”的取值可以为“page”、“service”或“data”,分别代表Page模板、Service模板、Data模板。为了便于表述,后文中我们将基于Page模板、Service模板、Data模板实现的Ability分别简称为Page、Service、Data。
{
"module": {
...
"abilities": [
{
...
"type": "page"
...
}
]
...
}
...
}
Page模板(以下简称“Page”)是FA唯一支持的模板,用于提供与用户交互的能力。一个Page可以由一个或多个AbilitySlice构成,AbilitySlice是指应用的单个页面及其控制逻辑的总和。
当一个Page由多个AbilitySlice共同构成时,这些AbilitySlice页面提供的业务能力应具有高度相关性。例如,新闻浏览功能可以通过一个Page来实现,其中包含了两个AbilitySlice:一个AbilitySlice用于展示新闻列表,另一个AbilitySlice用于展示新闻详情。Page和AbilitySlice的关系如图所示。
相比于桌面场景,移动场景下应用之间的交互更为频繁。通常,单个应用专注于某个方面的能力开发,当它需要其他能力辅助时,会调用其他应用提供的能力。例如,外卖应用提供了联系商家的业务功能入口,当用户在使用该功能时,会跳转到通话应用的拨号页面。与此类似,HarmonyOS支持不同Page之间的跳转,并可以指定跳转到目标Page中某个具体的AbilitySlice。
当发起导航的AbilitySlice和导航目标的AbilitySlice处于同一个Page时,您可以通过present()方法实现导航。如下代码片段展示通过点击按钮导航到其他AbilitySlice的方法:
第一步:创建一个ability_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <DependentLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent"> <Text ohos:id="$+id:text" ohos:width="match_content" ohos:height="match_content" ohos:text="Hello World" ohos:text_color="#000000" ohos:text_size="32fp" ohos:center_in_parent="true"/> <Button ohos:id="$+id:button" ohos:width="match_content" ohos:height="match_content" ohos:text="Next" ohos:text_size="19fp" ohos:text_color="#FFFFFF" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:right_padding="70vp" ohos:left_padding="70vp" ohos:center_in_parent="true" ohos:below="$id:text" ohos:margin="10vp"/> </DependentLayout>
第二步:创建background_button.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<corners
ohos:radius="100"/>
<solid
ohos:color="#007DFF"/>
</shape>
第三步:在layout目录下的“ability_main.xml”文件中,使用**background_element=“$graphic:background_button”**的方式引用“background_button.xml”文件:
<?xml version="1.0" encoding="utf-8"?> <DependentLayout <Button ohos:id="$+id:button" ohos:width="match_content" ohos:height="match_content" ohos:text="Next" ohos:text_size="19fp" ohos:text_color="#FFFFFF" ohos:top_padding="8vp" ohos:bottom_padding="8vp" ohos:right_padding="70vp" ohos:left_padding="70vp" ohos:center_in_parent="true" ohos:below="$id:text" ohos:margin="10vp" ohos:background_element="$graphic:background_button"/> </DependentLayout>
第四步:分别创建第二个要跳转的界面和代码模块
package com.example.myapplication.slice; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.colors.RgbColor; import ohos.agp.components.DependentLayout; import ohos.agp.components.Text; import ohos.agp.components.element.ShapeElement; import ohos.agp.utils.Color; import ohos.agp.components.DependentLayout.LayoutConfig; public class SecondAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_second); // 加载XML布局 } }
第五步:实现Ability之间的跳转
package com.example.myapplication.slice; import com.example.myapplication.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Button button = (Button) findComponentById(ResourceTable.Id_button); // 点击按钮跳转至第二个页面 button.setClickedListener(listener -> present(new SecondAbilitySlice(), new Intent())); } }
如果开发者希望在用户从导航目标AbilitySlice返回时,能够获得其返回结果,则应当使用presentForResult()实现导航。用户从导航目标AbilitySlice返回时,系统将回调onResult()来接收和处理返回结果,开发者需要重写该方法。返回结果由导航目标AbilitySlice在其生命周期内通过setResult()进行设置。
@Override protected void onStart(Intent intent) { ... Button button = ...; button.setClickedListener(listener -> presentForResult(new TargetSlice(), new Intent(), 0)); ... } @Override protected void onResult(int requestCode, Intent resultIntent) { if (requestCode == 0) { // Process resultIntent here. } }
系统管理或用户操作等行为均会引起Page实例在其生命周期的不同状态之间进行转换。Ability类提供的回调机制能够让Page及时感知外界变化,从而正确地应对状态变化(比如释放资源),这有助于提升应用的性能和稳健性。
Page生命周期的不同状态转换及其对应的回调,如图所示
Page生命周期和状态会经历以下这些过程:
当系统首次创建Page实例时,触发该回调,对于一个Page实例,该回调在其生命周期过程中仅仅触发一次,Page在该逻辑后进入INACTIVE(不活动的)状态,开发者必须要重写该方法,并在此配置默认展示的AbilitySlice
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(FooSlice.class.getName());
}
onActive()
Page会在进入INACTIVE状态后来到前台,然后系统调用此回调。Page在此之后进入ACTIVE状态,该状态是应用与用户交互的状态。Page将保持在此状态,除非某类事件发生导致Page失去焦点,比如用户点击返回键或导航到其他Page。当此类事件发生时,会触发Page回到INACTIVE状态,系统将调用onInactive()回调。此后,Page可能重新回到ACTIVE状态,系统将再次调用onActive()回调。因此,开发者通常需要成对实现onActive()和onInactive(),并在onActive()中获取在onInactive()中被释放的资源。
onInactive()
当Page失去焦点时,系统将调用此回调,此后Page进入INACTIVE状态。开发者可以在此回调中实现Page失去焦点时应表现的恰当行为。
onBackground()
如果Page不再对用户可见,系统将调用此回调通知开发者用户进行相应的资源释放,此后Page进入BACKGROUND状态。开发者应该在此回调中释放Page不可见时无用的资源,或在此回调中执行较为耗时的状态保存操作。
onForeground()
处于BACKGROUND状态的Page仍然驻留在内存中,当重新回到前台时(比如用户重新导航到此Page),系统将先调用onForeground()回调通知开发者,而后Page的生命周期状态回到INACTIVE状态。开发者应当在此回调中重新申请在onBackground()中释放的资源,最后Page的生命周期状态进一步回到ACTIVE状态,系统将通过onActive()回调通知开发者用户。
onStop()
系统将要销毁Page时,将会触发此回调函数,通知用户进行系统资源的释放。销毁Page的可能原因包括以下几个方面:
如何测试Page Ability的生命周期呢?主要是看Page Ability和AbilitySlice之间的生命周期状态是如何切换的。我们用以下代码来测试一下。
MainAbility代码:
package com.sudojava.firstdemo; import com.sudojava.firstdemo.slice.MainAbilitySlice; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; public class MainAbility extends Ability { public static final HiLogLabel HI_LOG_LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MainAbility"); @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(MainAbilitySlice.class.getName()); HiLog.info(HI_LOG_LABEL,"执行onStart方法------"); } @Override protected void onActive() { super.onActive(); HiLog.info(HI_LOG_LABEL,"执行onActive方法------"); } @Override protected void onInactive() { super.onInactive(); HiLog.info(HI_LOG_LABEL,"执行onInactive方法------"); } @Override protected void onBackground() { super.onBackground(); HiLog.info(HI_LOG_LABEL,"执行onBackground方法------"); } @Override protected void onForeground(Intent intent) { super.onForeground(intent); HiLog.info(HI_LOG_LABEL,"执行onForeground方法------"); } @Override protected void onStop() { super.onStop(); HiLog.info(HI_LOG_LABEL,"执行onStop方法------"); } }
MainAbilitySlice代码:
package com.sudojava.firstdemo.slice; import com.sudojava.firstdemo.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.aafwk.content.Operation; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; public class MainAbilitySlice extends AbilitySlice { public static final HiLogLabel HI_LOG_LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MainAbilitySlice"); @Override public void onStart(Intent intent1) { super.onStart(intent1); super.setUIContent(ResourceTable.Layout_ability_main); Button button = (Button)findComponentById(ResourceTable.Id_button); button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withDeviceId("") .withBundleName("com.sudojava.firstdemo") .withAbilityName("com.sudojava.firstdemo.Second") .build(); intent.setOperation(operation); startAbility(intent); HiLog.info(HI_LOG_LABEL,"hello world"); } }); } @Override protected void onActive() { super.onActive(); HiLog.info(HI_LOG_LABEL,"执行onActive方法------"); } @Override protected void onInactive() { super.onInactive(); HiLog.info(HI_LOG_LABEL,"执行onInactive方法------"); } @Override protected void onBackground() { super.onBackground(); HiLog.info(HI_LOG_LABEL,"执行onBackground方法------"); } @Override protected void onForeground(Intent intent) { super.onForeground(intent); HiLog.info(HI_LOG_LABEL,"执行onForeground方法------"); } @Override protected void onStop() { super.onStop(); HiLog.info(HI_LOG_LABEL,"执行onStop方法------"); } }
我们都知道Page由多个AbilitySlice共同构成时,每一个AbilitySlice之间都是可以传递值的,主要是通过Intent来传递,其中可以传递基本数据类型、数组、List包含泛型的集合数据。
第一步:在工程中的layout下新建一个布局文件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:orientation="vertical" ohos:alignment="vertical_center" > <Button ohos:id="$+id:button" ohos:width="match_parent" ohos:height="50vp" ohos:text_size="27fp" ohos:text="Page Slice 之间传递值" ohos:bottom_margin="15vp" ohos:right_padding="8vp" ohos:left_padding="8vp" ohos:background_element="$graphic:background_button" /> </DirectionalLayout>
第二步:在MainAbilitySlice中添加如下代码
public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainAbilitySlice"); @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Button button = (Button)findComponentById(ResourceTable.Id_button); button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Intent intent1 = new Intent(); intent1.setParam("name","jack"); intent1.setParam("code","10010"); present(new ResultSlice(),intent1); } }); }
第三步:创建一个ResultSlice的java类,继承AbilitySlice
package com.sudojava.slicedemo.slice; import com.sudojava.slicedemo.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Text; public class ResultSlice extends AbilitySlice { @Override protected void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_value); Text text = (Text)findComponentById(ResourceTable.Id_showtxt); if (intent!=null){ String name = intent.getStringParam("name"); String code = intent.getStringParam("code"); text.setText("接收的信息:Name:"+name+" Code:"+code); } } }
第四步:在layout布局文件夹下创建一个ability_value.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" ohos:alignment="vertical_center" > <Text ohos:height="match_content" ohos:width="match_content" ohos:id="$+id:showtxt" ohos:text_size="15fp" ohos:top_margin="100px" ohos:layout_alignment="horizontal_center" ohos:text="Hello World" /> </DirectionalLayout>
各个AbilitySlice之间除了可以传递值之外,也可以实现回传值的效果,主要是通过presentForResult的方法和重写onResult方法
第一步:在layout布局文件下创建ability_main.xml文件,并添加一个按钮控件,其中按钮的修饰文件也如下:
<Button ohos:id="$+id:button2" ohos:width="match_parent" ohos:height="50vp" ohos:text_size="27fp" ohos:text="Page Slice 回传值" ohos:bottom_margin="15vp" ohos:right_padding="8vp" ohos:left_padding="8vp" ohos:background_element="$graphic:background_button" /> --------------------------------------------------------------------------------------------------------------- <?xml version="1.0" encoding="UTF-8" ?> <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:shape="rectangle"> <corners ohos:radius="100" /> <solid ohos:color="#007DFF"/> </shape>
第二步:在MainAbilitySlice主类中添加如下代码:
//添加日志打印
private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainAbilitySlice");
//实现回传值
Button button2 = (Button)findComponentById(ResourceTable.Id_button2);
button2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent1 = new Intent();
intent1.setParam("name","jack");
presentForResult(new NextSlice(),intent1,123);
}
});
并且重写onResult的方法
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
if (requestCode==123){
String pwd = resultIntent.getStringParam("pwd");
HiLog.info(LABEL,"--pwd->>"+pwd);
}
}
第三步:在layout布局文件下创建ability_next.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" ohos:alignment="vertical_center" > <Button ohos:id="$+id:backbutton" ohos:width="match_parent" ohos:height="50vp" ohos:text_size="27fp" ohos:text="Back To Click" ohos:bottom_margin="15vp" ohos:right_padding="8vp" ohos:left_padding="8vp" ohos:background_element="$graphic:background_button" /> </DirectionalLayout>
第四步:在MainAbilitySlice主类中添加如下代码
private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MainAbilitySlice");
//打印输出日志
//实现回传值
Button button2 = (Button)findComponentById(ResourceTable.Id_button2);
button2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Intent intent1 = new Intent();
intent1.setParam("name","jack");
presentForResult(new NextSlice(),intent1,123);
}
});
重写onResult方法
@Override
protected void onResult(int requestCode, Intent resultIntent) {
super.onResult(requestCode, resultIntent);
if (requestCode==123){
String pwd = resultIntent.getStringParam("pwd");
HiLog.info(LABEL,"--pwd->>"+pwd);
}
}
第五步:创建一个java类NextSlice来继承AbilitySlice类,并添加如下代码:
public class NextSlice extends AbilitySlice { private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "NextSlice"); @Override protected void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_next); if (intent!=null){ String result = intent.getStringParam("name"); HiLog.info(LABEL,"--name-->>"+result); } Button button = (Button)findComponentById(ResourceTable.Id_backbutton); button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Intent intent1 = new Intent(); intent1.setParam("pwd", "123456"); setResult(intent1); terminate();//结束当前的Slice } }); } }
Ability之间是可以实现跳转的,也可以实现从一个Page Ability 跳转到指定的Slice。同时也可以实现传值的效果。
第一步:在layout文件夹下创建一个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:button1" ohos:height="match_content" ohos:text="跳转到第一个界面" ohos:text_size="30vp" ohos:background_element="$graphic:background_button" ohos:width="match_parent"/> <Button ohos:id="$+id:button2" ohos:height="match_content" ohos:text="跳转到第二个界面" ohos:top_margin="100px" ohos:text_size="30vp" ohos:background_element="$graphic:background_button" ohos:width="match_parent"/> </DirectionalLayout>
第二步:创建MainAbilitySlice类,添加两个Button,绑定事件。
package com.sudojava.abilitytoslice.slice; import com.sudojava.abilitytoslice.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Button button = (Button)findComponentById(ResourceTable.Id_button1); button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Intent intent = new Intent(); intent.setAction("todo1");//添加动作,跳转到指定的页面 startAbility(intent); } }); Button button2 = (Button)findComponentById(ResourceTable.Id_button2); button2.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Intent intent = new Intent(); intent.setAction("todo2");//添加动作,跳转到指定的页面 startAbility(intent); } }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
第三步:创建一个HomeAbility ,其中包含两个Slice,分别是:ShopAbilitySlice和HomeAbilitySlice,有两个对应的布局文件。
package com.sudojava.abilitytoslice; import com.sudojava.abilitytoslice.slice.HomeAbilitySlice; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; public class HomeAbility extends Ability { @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(HomeAbilitySlice.class.getName()); //添加对应的动作 addActionRoute("todo1","com.sudojava.abilitytoslice.slice.HomeAbilitySlice"); addActionRoute("todo2","com.sudojava.abilitytoslice.slice.ShopAbilitySlice"); } }
HomeAbilitySlice参考代码
package com.sudojava.abilitytoslice.slice; import com.sudojava.abilitytoslice.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; public class HomeAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_home); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
ShopAbilitySlice参考代码
package com.sudojava.abilitytoslice.slice; import com.sudojava.abilitytoslice.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; public class ShopAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_shop); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
第四步:创建两个对应的xml布局,分别是:ability_home.xml和ability_shop.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:text1" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_home" ohos:layout_alignment="horizontal_center" ohos:text="欢迎跳转到第一个页面" ohos:text_size="40vp" /> </DirectionalLayout> -------------------------------------------------------------------------------------------------------------- <?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:text2" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_ability_home" ohos:layout_alignment="horizontal_center" ohos:text="欢迎跳转到第二个页面" ohos:text_size="40vp" /> </DirectionalLayout>
第五步:在config.json的配置文件添加如下内容:
{ "app": { "bundleName": "com.sudojava.abilitytoslice", "vendor": "sudojava", "version": { "code": 1000000, "name": "1.0.0" } }, "deviceConfig": {}, "module": { "package": "com.sudojava.abilitytoslice", "name": ".MyApplication", "mainAbility": "com.sudojava.abilitytoslice.MainAbility", "deviceType": [ "phone" ], "distro": { "deliveryWithInstall": true, "moduleName": "entry", "moduleType": "entry", "installationFree": false }, "abilities": [ { "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ], "orientation": "unspecified", "name": "com.sudojava.abilitytoslice.MainAbility", "icon": "$media:icon", "description": "$string:mainability_description", "label": "$string:entry_MainAbility", "type": "page", "launchType": "standard" }, ---------------------------添加内容--------------------------------------- {//一个Page Ability中可以包含多个Slice,如果需要 "skills": [ { "actions": [ "todo1",//第一个动作 "todo2"//第二个动作 ] } ], "orientation": "unspecified", "name": "com.sudojava.abilitytoslice.HomeAbility", "icon": "$media:icon", "description": "$string:homeability_description", "label": "$string:entry_HomeAbility", "type": "page", "launchType": "standard" } ------------------------------添加内容------------------------------------ ] } }
第一步:在工程下的layout文件下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:button" ohos:height="match_content" ohos:width="match_content" ohos:background_element="$graphic:background_button" ohos:layout_alignment="horizontal_center" ohos:text="跳转到另一个Ability" ohos:text_size="30vp" /> </DirectionalLayout>
第二步:在MainAbilitySlice程序代码中添加以下代码,其中需要配置上日志文件
package com.sudojava.firstdemo.slice; import com.sudojava.firstdemo.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.aafwk.content.Operation; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel; public class MainAbilitySlice extends AbilitySlice { public static final HiLogLabel HI_LOG_LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MainAbilitySlice"); @Override public void onStart(Intent intent1) { super.onStart(intent1); super.setUIContent(ResourceTable.Layout_ability_main); Button button = (Button)findComponentById(ResourceTable.Id_button); button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { Intent intent = new Intent(); Operation operation = new Intent.OperationBuilder() .withDeviceId("") .withBundleName("com.sudojava.firstdemo") .withAbilityName("com.sudojava.firstdemo.Second") .build(); intent.setOperation(operation); startAbility(intent); HiLog.info(HI_LOG_LABEL,"hello world"); } }); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
第三步:创建另外一个Page Ability 点击File—Ability–empty page Ability(java),创建的过程中,系统会帮助我们创建一个对应的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>
第四步:创建button按钮的样式文件:background_button.xml
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<corners
ohos:radius="20px"
/>
<solid
ohos:color="#007DFF"/>
</shape>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。