引言
随着现代人生活节奏的加快,生活水平的不断提高,健康成为了眼下人们越来越关心的话题,尤其对于上班族,熬夜加班,晚睡早起,饮食不规律,缺乏锻炼,使得身体出现一定的亚健康问题。随着科技的发展,只有通过科学的方法了解身体的指标,给出合理的饮食,睡眠,运动等等建议,才能针对性的改善自身的健康问题,让我们每一个人回归健康,快乐的生活。
开发背景
人们对健康的要求越来越高,从单纯的健康饮食到健康运动,再到两者的结合。但是,饮食和运动之间的平衡一般人很难掌握,而我们这款APP将饮食、运动、以及自身身体状况(如体脂、体重、内脂等)有机结合起来。以体魄为根本,运动为支撑点,饮食为辅助,为每个人量身打造健康体魄,为实现健康中国努力。
需求分析
目前年轻人工作压力大,饮食基本靠外卖,锻炼就去健身房,无法将饮食和健身相结合,本APP旨在为每一位用户打造个性化的运动饮食计划。其功能主要分为两类:运动(包括如何运动、需要多大运动量)与饮食(早中晚餐吃什么,怎么制作等)。
主要功能介绍
健康+服务卡片主要包含三部分功能:
1、服务卡片(2*2)展示体脂仪主要测量数据(体重、身体年龄、BMI值);

点击该卡片,可以进入APP主页,查看更多指标数据。

2、服务卡片(4*4)展示体脂仪其他更为精准化的测量数据,并根据这些数据给出科学的饮食推荐;

点击“早餐推荐”或午餐推荐,可以跳转至推荐的饮食页面。

3、服务卡片(2*4)根据体脂仪测量的各项数据,给出合理化的,精准化的训练方案。

点击卡片右侧推荐的健身项目,进入对应的训练项目详解。

关键技术细节实现
一、创建“健康+”app工程
1、安装和配置DevEco Studio 2.1 Release
2、创建项目
1)打开DevEco Studio,File→New→New Project;

2)Choose your ability template,选择Empty Ability(Java),然后Next;

3)Configure your project,下列红色框中都是要填写或者选择的,其中Project Name,Package Name,Save Location可以自定义;Project Type注意选择Application;Compatible API Version注意选择最新SDK版本:SDK API Version 5;Device Type选择你使用的设备,我这里选择Phone。最后点击Finish,项目创建完成。

二、健康与饮食app服务卡片实现
第一步:卡片生成
1)在项目包名上右击,选择New→Service Widget

2)Choose a template for your service widget,选择Base的服务卡片,然后在Template下任意选择一种模版,点击Next

3)Configure Your Service Widget,其中Service Widget Name, Description可以自定义;Select Ability/New Ability使用默认的MainAbility; Type可以选择java或者js开发;SupportDimensions选择卡片样式(2*2、2*4以及4*4);点击Finish,完成服务卡片创建。

第二步:卡片布局的修改:
2*2服务卡片的布局

2*4服务卡片的布局

4*4服务卡片的布局

第三步:卡片信息创建、更新、删除


第四步、卡片在桌面的创建方式
长按app图标,弹出如下弹框

选择服务卡片,出现如下选择画面



选择添加到桌面,则桌面会出现对应的服务卡片



第五步:卡片的点击跳转实现


重难点解析
点击卡片中某个控件,跳转至指定页面
1)在MainAbility的创建卡片onCreateForm(Intent intent)方法中创建ComponentProvider对象
- ComponentProvider componentProvider=new ComponentProvider();
2)然后调用componentProvider.setIntentAgent(int viewId,IntentAgent intent)
其中第一个参数为控件ID,第二个参数为跳转指定页面传入的intent;
3)componentProvider.setIntentAgent(ResourceTable.Id_img_base2_4, startAbilityIntentAgent(ABILITY_NAME));
4)最后providerFormInfo.mergeActions(componentProvider);
5)startAbilityIntentAgent(ABILITY_NAME)参数为初入的ability名,字符串类型。
- private IntentAgent startAbilityIntentAgent(String abilityName) {
- Intent intent = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("")
- .withBundleName("com.zel.health")
- .withAbilityName(abilityName)
- .build();
- intent.setOperation(operation);
- List intentList = new ArrayList<>();
- intentList.add(intent);
- List flags = new ArrayList<>();
- flags.add(Flags.UPDATE_PRESENT_FLAG);
- IntentAgentInfo paramsInfo = new IntentAgentInfo(200, IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);
- IntentAgent intentAgent = IntentAgentHelper.getIntentAgent(this, paramsInfo);
- return intentAgent;
- }