如果覺得本文章幫助到你的話,點(diǎn)個(gè)贊。
目的
應(yīng)用啟動(dòng)頁(yè)在幾秒后跳轉(zhuǎn)
步驟
-
創(chuàng)建初始頁(yè)個(gè)Ability
image.png
創(chuàng)建后項(xiàng)目結(jié)構(gòu)圖
image.png - 在項(xiàng)目文件中找到
config.json文件,更改skills中的位置;從MainAbility中改到WelcomeAbility中
Skills主要設(shè)置起始頁(yè)為哪個(gè)Abliity

image.png
更改后的配置文件

image.png
- 在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);
}
}
- 編寫
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>
-
執(zhí)行應(yīng)用
執(zhí)行效果
image.png
image.png
如果讀者發(fā)現(xiàn)內(nèi)容有誤的地方私信我:郵箱2276284591@qq.com
如果覺得本文章幫助到你的話,點(diǎn)個(gè)贊。



