如果觉得本文章帮助到你的话,点个赞。
目的
应用启动页在几秒后跳转
步骤
-
创建初始页个Ability
image.png
创建后项目结构图
image.png - 在项目文件中找到
config.json文件,更改skills中的位置;从MainAbility中改到WelcomeAbility中
Skills主要设置起始页为哪个Abliity

image.png
更改后的配置文件

image.png
- 在WelcomeAbilitySlice文件中的onActive()方法中写入如下代码
如需了解更多信息请查阅鸿蒙文档
public class WelcomeAbilitySlice extends AbilitySlice {
// 定义首页显示的时间---
private static final long DELAY = 3000;
private TimerTask task;
@Override
public void onActive() {
super.onActive();
// 初始化Intent
final Intent intent = new Intent();
// 通过Intent中的OperationBuilder类构造operation对象,指定设备标识(空串表示当前设备)、应用包名、Ability名称
// withBundleName为读者自己项目的包名
// withAbilityName为读者需要跳转到主界面的Ability的名字
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName("com.myapplication")
.withAbilityName("com.myapplication.MainAbility")
.build();
// 把operation设置到intent中
intent.setOperation(operation);
Timer timer=new Timer();
task=new TimerTask() {
@Override
public void run(){
startAbility(intent);//执行
}
};
// 设置延迟执行的时间
timer.schedule(task,DELAY);
}
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_welcome);
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
- 编写
ability_welcome.xml放图片
hello为图片资源
<?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">
<Image
ohos:height="match_parent"
ohos:width="match_parent"
ohos:image_src="$media:hello"/>
</DirectionalLayout>
-
执行应用
执行效果
image.png
image.png
如果读者发现内容有误的地方私信我:邮箱2276284591@qq.com
如果觉得本文章帮助到你的话,点个赞。



