鸿蒙应用开始页跳转实现(基于Java)

如果觉得本文章帮助到你的话,点个赞。

目的

应用启动页在几秒后跳转

步骤

  1. 创建初始页个Ability


    image.png

    创建后项目结构图


    image.png
  2. 在项目文件中找到config.json文件,更改skills中的位置;从MainAbility中改到WelcomeAbility中

Skills主要设置起始页为哪个Abliity

image.png

更改后的配置文件


image.png
  1. 在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);
    }
}

  1. 编写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>
  1. 执行应用
    执行效果


    image.png

    image.png

如果读者发现内容有误的地方私信我:邮箱2276284591@qq.com
如果觉得本文章帮助到你的话,点个赞。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容