HarmonyOS原子化服务卡片immersive(java)体验

一、技术相关

项目名称: immersive

项目语言: JAVA

体验模板:immersive

工具:deveco studio

  1. 体验内容

天气预报样式

小卡片 中卡片 大卡片

  1. 效果展示

视频

图片

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(1)

四、开发步骤

新建一个java的应用项目

然后构建完成项目后在jltf_template下new->module

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(2)

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(3)

选择的Full screen(java)模板

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(4)

选择项目的empty,新建Service Widget

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(5)

选择image and information模板,点击下一步

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(6)

选择卡片类型,点击finish

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(7)

登录你的账号然后启动模拟器即可实现效果

五、项目结构介绍

harmonyos官网开发者预览版(HarmonyOS原子化服务卡片immersive)(8)

该项目采用了项目模板full srceen和卡片模板immersive,通过两种卡片结合更容易设计

六、关键代码(MainAbility)与样式实现

public class MainAbility extends Ability {

public static final int DEFAULT_DIMENSION_2X2 = 2;public static final int DIMENSION_1X2 = 1;public static final int DIMENSION_2X4 = 3;public static final int DIMENSION_4X4 = 4;private static final int INVALID_FORM_ID = -1;private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, MainAbility.class.getName());private String topWidgetSlice;@Overridepublic void onStart(intent intent) {super.onStart(intent);super.setMainRoute(MainAbilitySlice.class.getName());if (intentFromWidget(intent)) {topWidgetSlice = getRoutePageSlice(intent);if (topWidgetSlice != null) {setMainRoute(topWidgetSlice);}}stopAbility(intent);}@Overrideprotected ProviderFormInfo onCreateForm(Intent intent) {HiLog.info(TAG, "onCreateForm");long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);String formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);int dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);HiLog.info(TAG, "onCreateForm: formId=" formId ",formName=" formName);FormControllerManager formControllerManager = FormControllerManager.getInstance(this);FormController formController = formControllerManager.getController(formId);formController = (formController == null) ? formControllerManager.createFormController(formId,formName, dimension) : formController;if (formController == null) {HiLog.error(TAG, "Get null controller. formId: " formId ", formName: " formName);return null;}return formController.bindFormData();}@Overrideprotected void onUpdateForm(long formId) {HiLog.info(TAG, "onUpdateForm");super.onUpdateForm(formId);FormControllerManager formControllerManager = FormControllerManager.getInstance(this);FormController formController = formControllerManager.getController(formId);formController.updateFormData(formId);}@Overrideprotected void onDeleteForm(long formId) {HiLog.info(TAG, "onDeleteForm: formId=" formId);super.onDeleteForm(formId);FormControllerManager formControllerManager = FormControllerManager.getInstance(this);formControllerManager.deleteFormController(formId);}@Overrideprotected void onTriggerFormEvent(long formId, String message) {HiLog.info(TAG, "onTriggerFormEvent: " message);super.onTriggerFormEvent(formId, message);FormControllerManager formControllerManager = FormControllerManager.getInstance(this);FormController formController = formControllerManager.getController(formId);formController.onTriggerFormEvent(formId, message);}@Overridepublic void onNewIntent(Intent intent) {if (intentFromWidget(intent)) { // Only response to it when starting from a service widget.String newWidgetSlice = getRoutePageSlice(intent);if (topWidgetSlice == null || !topWidgetSlice.equals(newWidgetSlice)) {topWidgetSlice = newWidgetSlice;restart();}}}private boolean intentFromWidget(Intent intent) {long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);return formId != INVALID_FORM_ID;}private String getRoutePageSlice(Intent intent) {long formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);if (formId == INVALID_FORM_ID) {return null;}FormControllerManager formControllerManager = FormControllerManager.getInstance(this);FormController formController = formControllerManager.getController(formId);if (formController == null) {return null;}Class<? extends AbilitySlice> clazz = formController.getRoutePageSlice(intent);if (clazz == null) {return null;}return clazz.getName();}}

七、完整代码地址:

https://gitee.com/jltfcloudcn/jump_to/tree/master/immersive

,