HarmonyOS鸿蒙Next自定义开屏页示例代码
HarmonyOS鸿蒙Next自定义开屏页示例代码 市面上几乎所有软件都有自己的开屏页,以下是鸿蒙实现的示例:
新建一个开屏页文件 SplashIndex.ets
,写上自己想要的内容,并将软件首页命名为 MainIndex.ets
:
import common from '@ohos.app.ability.common'
import { router } from '@kit.ArkUI'
import { updateManager } from '@kit.StoreKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
let context: common.UIAbilityContext = getContext() as common.UIAbilityContext;
@Entry
@Component
struct SplashIndex {
jumpToMain() {
setTimeout(() => {
// 检查更新
try {
updateManager.checkAppUpdate(context)
.then((checkResult: updateManager.CheckUpdateResult) => {
hilog.info(0, 'TAG', "Succeeded in checking Result updateAvailable:" + checkResult.updateAvailable);
try {
updateManager.showUpdateDialog(context)
.then((resultCode: updateManager.ShowUpdateResultCode) => {
hilog.info(0, 'TAG', "Succeeded in showing UpdateDialog resultCode:" + resultCode);
})
.catch((error: BusinessError) => {
hilog.error(0, 'TAG', `showUpdateDialog onError.code is ${error.code}, message is ${error.message}`);
});
} catch (error) {
hilog.error(0, 'TAG', `showUpdateDialog onError.code is ${error.code}, message is ${error.message}`);
}
}).catch((error: BusinessError) => {
hilog.error(0, 'TAG', `checkAppUpdate onError.code is ${error.code}, message is ${error.message}`);
});
} catch (error) {
hilog.error(0, 'TAG', `checkAppUpdate onError.code is ${error.code}, message is ${error.message}`);
}
router.replaceUrl({url: "pages/MainIndex"})
}, 2000)
}
aboutToDisappear() {
clearTimeout()
}
build() {
RelativeContainer(){
Row({space:10}){
Image($r('app.media.appIcon')).width(60).height(60)
Column({space:5}){
Text("APP名称").fontSize(22).fontColor("#333333")
Text("APP简介").fontSize(14).fontColor("#999999")
}.alignItems(HorizontalAlign.Start)
}.alignRules({
bottom: {anchor: "__container__", align: VerticalAlign.Bottom},
middle: { anchor: '__container__', align: HorizontalAlign.Center }
}).margin({
bottom:30
})
}
.width('100%')
.height('100%').backgroundColor(Color.White)
}
}
以上是一个简单的开屏页示例,该页面在启动的同时进行了检查更新的操作,隐私政策弹窗示例可参考后续文章
更多关于HarmonyOS鸿蒙Next自定义开屏页示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
他这示例为啥不用自己推荐的Navigation呢?我注意到Navigation好像无法关闭首页。
更多关于HarmonyOS鸿蒙Next自定义开屏页示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
感谢分享~
HarmonyOS鸿蒙Next自定义开屏页的示例代码如下:
import { Ability, AbilityContext, Intent } from '@ohos.application.Ability';
import { Window, WindowType } from '@ohos.window';
import { Logger } from '@ohos.log';
const TAG: string = 'SplashAbility';
export default class SplashAbility extends Ability {
private window: Window | null = null;
onCreate(want: Intent, launchParam: AbilityContext.LaunchParam): void {
Logger.info(TAG, 'SplashAbility onCreate');
this.window = this.context.createWindow('splash', WindowType.TYPE_APP);
this.window.loadContent('pages/splash', (err) => {
if (err) {
Logger.error(TAG, `Failed to load content. Code is ${err.code}, message is ${err.message}`);
return;
}
this.window?.show();
});
}
onDestroy(): void {
Logger.info(TAG, 'SplashAbility onDestroy');
if (this.window) {
this.window.destroy();
}
}
}
在resources/base/profile/main_pages.json
中配置页面路由:
{
"src": [
"pages/splash"
]
}
在resources/base/layout/splash.xml
中定义开屏页布局:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:background_element="#FF0000"
ohos:orientation="vertical">
<Text
ohos:width="match_content"
ohos:height="match_content"
ohos:text="Welcome to HarmonyOS"
ohos:text_size="30fp"
ohos:text_color="#FFFFFF"
ohos:layout_alignment="center"/>
</DirectionalLayout>
在config.json
中配置Ability:
{
"app": {
"bundleName": "com.example.splash",
"vendor": "example",
"version": {
"code": 1,
"name": "1.0"
}
},
"deviceConfig": {},
"module": {
"package": "com.example.splash",
"name": ".MyApplication",
"deviceType": [
"phone"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "entry",
"moduleType": "entry"
},
"abilities": [
{
"name": "SplashAbility",
"icon": "$media:icon",
"label": "$string:app_name",
"launchType": "standard",
"orientation": "unspecified",
"visible": true,
"skills": [
{
"actions": [
"action.system.home"
],
"entities": [
"entity.system.home"
]
}
]
}
]
}
}
以上代码展示了如何在HarmonyOS鸿蒙Next中实现自定义开屏页。
在HarmonyOS鸿蒙Next中,自定义开屏页可以通过AbilitySlice
实现。以下是一个简单的示例代码:
public class SplashAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
// 设置布局文件
setUIContent(ResourceTable.Layout_splash_layout);
// 延迟3秒后跳转到主页面
getUITaskDispatcher().delayDispatch(() -> {
Intent mainIntent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName(getBundleName())
.withAbilityName(MainAbility.class.getName())
.build();
mainIntent.setOperation(operation);
startAbility(mainIntent);
terminate();
}, 3000);
}
}
在config.json
中配置SplashAbilitySlice
为启动页:
"abilities": [
{
"name": ".SplashAbility",
"srcEntrance": "./ets/SplashAbilitySlice.java",
"launchType": "standard",
"visible": true
}
]
此代码实现了一个3秒延迟后跳转到主页面的开屏页。