鴻蒙應(yīng)用開始頁(yè)跳轉(zhuǎn)實(shí)現(xiàn)(基于Java)

如果覺得本文章幫助到你的話,點(diǎn)個(gè)贊。

目的

應(yīng)用啟動(dòng)頁(yè)在幾秒后跳轉(zhuǎn)

步驟

  1. 創(chuàng)建初始頁(yè)個(gè)Ability


    image.png

    創(chuàng)建后項(xiàng)目結(jié)構(gòu)圖


    image.png
  2. 在項(xiàng)目文件中找到config.json文件,更改skills中的位置;從MainAbility中改到WelcomeAbility中

Skills主要設(shè)置起始頁(yè)為哪個(gè)Abliity

image.png

更改后的配置文件


image.png
  1. 在WelcomeAbilitySlice文件中的onActive()方法中寫入如下代碼

如需了解更多信息請(qǐng)查閱鴻蒙文檔


public class WelcomeAbilitySlice extends AbilitySlice {
    // 定義首頁(yè)顯示的時(shí)間---
    private static final long DELAY = 3000;
 
    private TimerTask task;

    @Override
    public void onActive() {
        super.onActive();
        // 初始化Intent
        final Intent intent = new Intent();

        // 通過(guò)Intent中的OperationBuilder類構(gòu)造operation對(duì)象,指定設(shè)備標(biāo)識(shí)(空串表示當(dāng)前設(shè)備)、應(yīng)用包名、Ability名稱
        // withBundleName為讀者自己項(xiàng)目的包名
        // withAbilityName為讀者需要跳轉(zhuǎn)到主界面的Ability的名字
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.myapplication")
                .withAbilityName("com.myapplication.MainAbility")
                .build();

        // 把operation設(shè)置到intent中
        intent.setOperation(operation);

        Timer timer=new Timer();

        task=new TimerTask() {
            @Override
            public void run(){
                startAbility(intent);//執(zhí)行
            }
        };

        // 設(shè)置延遲執(zhí)行的時(shí)間
        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. 執(zhí)行應(yīng)用
    執(zhí)行效果


    image.png

    image.png

如果讀者發(fā)現(xiàn)內(nèi)容有誤的地方私信我:郵箱2276284591@qq.com
如果覺得本文章幫助到你的話,點(diǎn)個(gè)贊。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容