当前位置:网站首页>How can the sports app keep the end-to-side background alive to make the sports record more complete?
How can the sports app keep the end-to-side background alive to make the sports record more complete?
2022-06-30 10:45:00 【HMS Core】
When you're working out , Have you ever encountered such a situation ? Worked hard for hours , But found App Stopped running , This movement has not been recorded App On , Thus, the opportunity to view the complete motion data is lost ?
Sports App It is through the sensor of mobile phone or wearable device , To identify the motion state and feed back to the user ,App Whether the mobile phone can always run in the background is a key factor affecting the integrity of sports data . In order to meet the needs of users to view complete motion data , Sports App All hope to keep alive in the background of the equipment , And the motion data of users are recorded in real time through sensors . But most mobile phone manufacturers are trying to save power , Once the application is in the background, it will be restricted or even forcibly closed by the system , As a result, the motion record finally presented to the user is incomplete .
Sports App To realize the end-to-end and back-end protection , There are usually two solutions :
Guide the user to manually set the keep alive on the mobile phone , If battery optimization is turned off , allow App Background operation . The disadvantage of this method is that the operation steps are complex , User learning costs are high .
Through integration Huawei sports health service To solve this problem , The sports health service provides back-end support for keeping alive sports records API, After integrating this capability, the application can keep running in the background of Huawei mobile phones during the user's exercise , So as to realize the uninterrupted movement record during the user's exercise .
How to realize the backstage keep alive function ? Here are the detailed integration steps .
Integration steps
Please refer to The development of preparation Complete the application Health Kit service , Check the data permission required for the product and integrate SDK.
To call the background keep alive function, you need to apply for the permission to read the motion records , Again Get user authorization Complete permission application .
To ensure that your application is not frozen by the system , You need to start a front desk service Foreground services, Call... In the foreground service ActivityRecordsController Method to create a motion record that is allowed to run in the background ;
call ActivityRecordsController Of beginActivityRecord The interface starts to allow motion recording to run in the background , By default, the application will be allowed to run in the background 10 minute ;
// Please note that this by Activity object ActivityRecordsController activityRecordsController = HuaweiHiHealth.getActivityRecordsController(this); // 1. New tectonic movement record start time long startTime = Calendar.getInstance().getTimeInMillis(); // 2. structure ActivityRecord object , Set the motion recording start time ActivityRecord activityRecord = new ActivityRecord.Builder() .setId("MyBeginActivityRecordId") .setName("BeginActivityRecord") .setDesc("This is ActivityRecord begin test!") .setActivityTypeId(HiHealthActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .build(); // 3. Build the page displayed during the running of application motion record , MyActivity It needs to be replaced with its own Activity class ComponentName componentName = new ComponentName(this, MyActivity.class);// 4. Build a motion recording background running state change listener OnActivityRecordListener activityRecordListener = new OnActivityRecordListener() { @Override public void onStatusChange(int statusCode) { Log.i("ActivityRecords", "onStatusChange statusCode:" + statusCode); }};// 5. Call to start a new motion record API Interface beginActivityRecordTask<Void> task1 = activityRecordsController.beginActivityRecord(activityRecord, componentName, activityRecordListener); // 6. Add startup ActivityRecord success task1.addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Log.i("ActivityRecords", "MyActivityRecord begin success"); } // 7. Add startup ActivityRecord Failure }).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); } });- If the user moves for a long time , Every adjacent 10 minute ( Less than 10 minute ) Need to call ActivityRecordsController Of continueActivityRecord The interface continues to apply for background preservation 10 minute ;
// Please note that this by Activity object ActivityRecordsController activityRecordsController = HuaweiHiHealth.getActivityRecordsController(this); // call continueActivityRecord Method to apply for permission to run in the background for the specified motion record , The parameter for ActivityRecord Of ID character string 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"); }});- When the user's movement ends , call ActivityRecordsController Of endActivityRecord The interface stops the motion recording , At the same time, cancel the application background preservation ;
// Please note that this by Activity object final ActivityRecordsController activityRecordsController = HuaweiHiHealth.getActivityRecordsController(this);// call endActivityRecord Interface stops motion recording , The parameter for ActivityRecord Of ID String or null// The parameter for ID When the string , Stop the current application assignment ID Movement record of // The parameter for null when , Stop all current unstopped motion records of the application 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 motion records that have stopped successfully 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 { // Failed to stop and return successfully null 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); }});It should be noted that , Because the end and side backstage are kept alive API Belongs to sensitive permission , The access of sports applications requires manual review , Ensure data security 、 Only process compliance can be put on the shelves .
obtain End side background liveability development document
Huawei Sports health scenario solutions
Learn more >>
visit Official website of Huawei developer Alliance
obtain Development guidance document
Huawei mobile service open source warehouse address :GitHub、Gitee
Pay attention to our , The first time to understand HMS Core Latest technical information ~
边栏推荐
- Anhui "requirements for design depth of Hefei fabricated building construction drawing review" was printed and distributed; Hebei Hengshui city adjusts the pre-sale license standard for prefabricated
- 技能梳理[email protected]在oled上控制一条狗的奔跑
- 05_ Node JS file management module FS
- Double-DQN笔记
- 半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
- 【深度学习】深度学习检测小目标常用方法
- 那个程序员,被打了。
- 六月集训(第30天) —— 拓扑排序
- Apple's 5g chip was revealed to have failed in research and development, and the QQ password bug caused heated discussion. Wei Lai responded to the short selling rumors. Today, more big news is here
- 转卡通学习笔记
猜你喜欢

The programmer was beaten.

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计

微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条...
[email protected]基于51系列单片机的智能仪器教具"/>技能梳理[email protected]基于51系列单片机的智能仪器教具

软件测试工程师面试基础题(应届生和测试小菜必备)最基础的面试题

scratch绘制正方形 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月

苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...

Qt之实现动效导航栏

再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大

Implementation of monitor program with assembly language
随机推荐
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
Go -- standard library sort package
The AOV function of R language was used for repeated measures ANOVA (one intra group factor and one inter group factor) and interaction Plot function and boxplot to visualize the interaction
Curl --- the request fails when the post request parameter is too long (more than 1024b)
Q-Learning笔记
ArcGIS Pro scripting tool (6) -- repairing CAD layer data sources
在 sCrypt 中实现高效的椭圆曲线点加法和乘法
Musk has more than 100 million twitter fans, but he has been lost online for a week
文件共享服务器
Qt之实现QQ天气预报窗体翻转效果
Mysql database foundation: TCL transaction control language
潘多拉 IOT 开发板学习(HAL 库)—— 实验1 跑马灯(RGB)实验(学习笔记)
pytorch 筆記 torch.nn.BatchNorm1d
Robotframework learning notes: environment installation and robotframework browser plug-in installation
R语言aov函数进行重复测量方差分析(Repeated measures ANOVA、其中一个组内因素和一个组间因素)、分别使用interaction.plot函数和boxplot对交互作用进行可视化
Mysql database foundation: views and variables
R language plot visualization: use plot to visualize the prediction confidence of the multi classification model, the prediction confidence of each data point of the model in the 2D grid, and the conf
[deep learning] common methods for deep learning to detect small targets
Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...
程序员需知的 59 个网站