当前位置:网站首页>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-29 13:29: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 ~
边栏推荐
- Simple introduction to matlab
- iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
- Netdata mail alarm configuration
- RT thread memory management
- Uber前安全主管面临欺诈指控 曾隐瞒数据泄露事件
- Detailed explanation on configuration and commissioning of third-party servo of Beifu TwinCAT -- Taking Huichuan is620n as an example
- win32版俄罗斯方块(学习MFC必不可少)
- Tutorial on building pytoch model from zero (V) writing training process -- some basic configurations
- async原理实现
- Another "provincial capital university", coming!
猜你喜欢

mysql调优

使用 Gerrit + Zadig 实现主干开发主干发布(含字节飞书实践)
![Equidistant segmentation of surface rivers in ArcGIS [gradient coloring, pollutant diffusion]](/img/05/18fb41f78b9b57175d50dfece65535.png)
Equidistant segmentation of surface rivers in ArcGIS [gradient coloring, pollutant diffusion]

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file

SCHIEDERWERK電源維修SMPS12/50 PFC3800解析

RT thread memory management

QT signal and slot

Hystrix circuit breaker

CVPR 2022 | 未知目标检测模块STUD:学习视频中的未知目标

安装typescript环境并开启VSCode自动监视编译ts文件为js文件
随机推荐
Comparison table of LR and Cr button batteries
Netdata mail alarm configuration
Schiederwerk Power Supply repair smps12 / 50 pfc3800 Analysis
Evaluation of powerful and excellent document management software: image management, book management and document management
【无标题】安装依赖报错:Refusing to install package with name “***“ under a package
Equidistant segmentation of surface rivers in ArcGIS [gradient coloring, pollutant diffusion]
leetcode 522. 最长特殊序列 II
C binary tree structure definition and node value addition
C # implementation of binary tree non recursive middle order traversal program
@Table爆红
CVPR 2022 | 未知目标检测模块STUD:学习视频中的未知目标
Write a shell script to find the "reverse order" of a number“
C#二叉树结构定义、添加节点值
clickhouse数据库使用jdbc存储毫秒和纳秒
CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
强大、优秀的文件管理软件评测:图片管理、书籍管理、文献管理
Acwing 234 abandoning testing
AOSP ~ logcat persistence
UI file introduction in QT