当前位置:网站首页>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-28 16:06:00 【51CTO】
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 .
- This problem can be solved by integrating Huawei sports health services , 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 preparation completion 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 , Then obtain the user authorization to complete the 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 beginActivityRecord
Task<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);
}
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 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");
}
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 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);
}
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
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 .
Learn more >>
obtain Development guidance document
Huawei mobile service open source warehouse address : GitHub
Pay attention to our , The first time to understand HMS Core Latest technical information ~
边栏推荐
- Sample explanation of batch inserting data using MySQL bulkloader
- 10: 00 interview, came out at 10:02, the question is really too
- QT interface library
- 北京有哪些牛逼的中小型公司?
- 分布式 CAP 定理的前世今生
- MongoDB 在腾讯零售优码中的应用
- Flutter simply implements multilingual internationalization
- 3. Caller 服务调用 - dapr
- What are the most powerful small and medium-sized companies in Beijing?
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
猜你喜欢

See how the interface control devaxpress WinForms creates a virtual keyboard

NAACL 2022 | 机器翻译SOTA模型的蒸馏
![[leetcode] 13. Roman numeral to integer](/img/3c/7c57d0c407f5302115f69f44b473c5.png)
[leetcode] 13. Roman numeral to integer

全球陆续拥抱Web3.0,多国已明确开始抢占先机

薅羊毛的机会了,点个“赚”即有机会赚取高额佣金

Among US private server setup

隐私计算 FATE - 离线预测

Fleet |「后台探秘」第 3 期:状态管理

RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52

MIPS assembly language learning-01-sum of two numbers, environment configuration and how to run
随机推荐
看界面控件DevExpress WinForms如何创建一个虚拟键盘
早晨有些犹豫
分布式 CAP 定理的前世今生
字节跳动数据平台技术揭秘:基于ClickHouse的复杂查询实现与优化
榜单首发——线控制动「新周期」,本土供应商市场竞争力TOP10
机器学习之深度学习卷积神经网络,实现基于CNN网络的手写字体识别
Analysis of PostgreSQL storage structure
【推荐系统】多任务学习之ESMM模型(更新ing)
Visual Studio 2019软件安装包和安装教程
【MySQL】表连接为什么比子查询快
A bug liver a week I can't help mentioning issue
RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52
Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(上)
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
Change exchange (dynamic planning)
openGauss内核:SQL解析过程分析
PostgreSQL enables grouping statistics by year, month, day, week, hour, minute and second
平台即代码的未来是Kubernetes扩展
PostgreSQL 存储结构浅析
IPDK — Overview