您的应用程序后台(应用程序后台您的用户数据)「你的应用程序」

一篇关于如何在健身应用程序的后台跟踪锻炼数据的文章
由于健身应用程序已停止在后台运行,因此忘记锻炼会非常令人沮丧
当您在锻炼期间关闭屏幕或在前面安装另一个应用程序来听音乐或观看视频时,就会发生这种情况
谈论你所有的汗水和努力都将浪费
健身应用程序通过使用手机或可穿戴设备上的传感器实时识别和显示用户的锻炼状态来工作
只有能够在后台持续运行,他们才能获取并显示完整的锻炼记录给用户
由于大多数用户会在锻炼期间关闭屏幕或使用其他应用程序,因此它一直是健身应用程序在后台保持活力的必备功能
但是,为了节省电池电量,大多数手机在后台运行后都会限制甚至强制关闭应用程序,从而导致锻炼数据不完整
在构建您自己的健身应用程序时,请务必牢记这一限制
有两种久经考验的方法可以让健身应用程序在后台运行:指导用户手动配置手机或可穿戴设备上的设置,例如禁用电池优化或允许特定应用在后台运行
然而,这个过程可能很麻烦,而且不容易遵循
或者,将开发工具集成到您的应用程序中,例如Health Kit,它提供的 API 允许您的应用程序在锻炼期间在后台继续运行,而不会丢失任何锻炼数据的跟踪
下面详细介绍这个套件的集成过程
整合程序1. 开始前,在华为开发者联盟申请Health Kit,选择需要的数据范围,集成Health SDK
2. 获得用户授权,申请范围读写锻炼记录
3. 开启前台服务,防止你的app被系统冻结,并在前台服务中调用ActivityRecordsController创建一个可以在后台运行的锻炼记录
4、调用ActivityRecordsController的beginActivityRecord,开始锻炼记录
默认情况下,应用程序将被允许在后台运行 10 分钟
// Note that this refers to an Activity object.ActivityRecordsController activityRecordsController = HuaweiHiHealth.getActivityRecordsController(this); // 1. Build the start time of a new workout record.long startTime = Calendar.getInstance().getTimeInMillis(); // 2. Build the ActivityRecord object and set the start time of the workout record.ActivityRecord activityRecord = new ActivityRecord.Builder() .setId("MyBeginActivityRecordId") .setName("BeginActivityRecord") .setDesc("This is ActivityRecord begin test!") .setActivityTypeId(HiHealthActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .build(); // 3. Construct the screen to be displayed when the workout record is running in the background. Note that you need to replace MyActivity with the Activity class of the screen.ComponentName componentName = new ComponentName(this, MyActivity.class);// 4. Construct a listener for the status change of the workout record.OnActivityRecordListener activityRecordListener = new OnActivityRecordListener() { @Override public void onStatusChange(int statusCode) { Log.i("ActivityRecords", "onStatusChange statusCode:" + statusCode); }};// 5. Call beginActivityRecord to start the workout record.Task<Void> task1 = activityRecordsController.beginActivityRecord(activityRecord, componentName, activityRecordListener); // 6. ActivityRecord is successfully started.task1.addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Log.i("ActivityRecords", "MyActivityRecord begin success"); } // 7. ActivityRecord fails to be started.}).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { String errorCode = e.getMessage(); String errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode)); Log.i("ActivityRecords", errorCode + ": " + errorMsg); } });5.如果锻炼持续时间超过10分钟,则在每次10分钟结束前调用ActivityRecordsController的continueActivityRecord,申请继续锻炼10分钟
// Note that this refers to an Activity object.ActivityRecordsController activityRecordsController = HuaweiHiHealth.getActivityRecordsController(this); // Call continueActivityRecord and pass the workout record ID for the record to continue in the background.Task<Void> endTask = activityRecordsController.continueActivityRecord("MyBeginActivityRecordId");endTask.addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Log.i("ActivityRecords", "continue backgroundActivityRecord was successful!"); }}).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { Log.i("ActivityRecords", "continue backgroundActivityRecord error"); }});6.当用户完成锻炼后,调用ActivityRecordsController的endActivityRecord停止记录,并停止在后台保持活动
// Note that this refers to an Activity object.final ActivityRecordsController activityRecordsController = HuaweiHiHealth.getActivityRecordsController(this);// Call endActivityRecord to stop the workout record. The input parameter is null or the ID string of ActivityRecord.// Stop a workout record of the current app by specifying the ID string as the input parameter.// Stop all workout records of the current app by specifying null as the input parameter.Task<List<ActivityRecord>> endTask = activityRecordsController.endActivityRecord("MyBeginActivityRecordId");endTask.addOnSuccessListener(new OnSuccessListener<List<ActivityRecord>>() { @Override public void onSuccess(List<ActivityRecord> activityRecords) { Log.i("ActivityRecords","MyActivityRecord End success"); // Return the list of workout records that have stopped. if (activityRecords.size() > 0) { for (ActivityRecord activityRecord : activityRecords) { DateFormat dateFormat = DateFormat.getDateInstance(); DateFormat timeFormat = DateFormat.getTimeInstance(); Log.i("ActivityRecords", "Returned for ActivityRecord: " + activityRecord.getName() + "\n\tActivityRecord Identifier is " + activityRecord.getId() + "\n\tActivityRecord created by app is " + activityRecord.getPackageName() + "\n\tDescription: " + activityRecord.getDesc() + "\n\tStart: " + dateFormat.format(activityRecord.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(activityRecord.getStartTime(TimeUnit.MILLISECONDS)) + "\n\tEnd: " + dateFormat.format(activityRecord.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(activityRecord.getEndTime(TimeUnit.MILLISECONDS)) + "\n\tActivity:" + activityRecord.getActivityType()); } } else { // null will be returned if the workout record hasn't stopped. Log.i("ActivityRecords","MyActivityRecord End response is null"); } }}).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { String errorCode = e.getMessage(); String errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode)); Log.i("ActivityRecords",errorCode + ": " + errorMsg); }});请注意,调用 API 以使您的应用程序在后台运行是一项敏感操作,需要手动批准
在申请发布之前,请确保您的应用满足数据安全和合规要求
结论健身应用程序通过为用户提供量身定制的健身方案、帮助他们设定切合实际的健身目标以及跟踪用户的健康和健身状况,在保持用户健康和积极性方面发挥着至关重要的作用
要实时监控锻炼记录,健身应用程序必须能够连续运行,即使在屏幕已关闭,或者在前面打开另一个应用程序运行时也是如此
这样做的一种方法是集成一项服务,该服务提供在后台保持锻炼记录的能力,这有助于显着改善用户使用健身应用程序的体验
让我们开始吧
您的应用程序后台(应用程序后台您的用户数据)
(图片来源网络,侵删)

联系我们

在线咨询:点击这里给我发消息