活动识别API服务开发
活动识别API服务开发
要使用华为活动识别服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中。
指定应用权限
- 在Android Q以下版本使用活动识别需要在“AndroidManifest.xml”文件中配置以下权限:
- <uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION"/>
- 在Android Q及以上版本中,需要在“AndroidManifest.xml”文件中申请以下权限:
. <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
说明
以上活动识别相关权限属于危险权限,使用时需要动态申请。
注册静态广播
示例代码中活动识别服务的活动状态更新信息需要广播接收,因此需要在“AndroidManifest.xml”文件中注册广播接收器。
- <!--注册活动识别服务广播接收器-->
- <receiver
- android:name=".location.fusedlocation.LocationBroadcastReceiver"
- android:exported="true">
- <intent-filter>
- <action android:name="com.huawei.hmssample.location.LocationBroadcastReceiver.ACTION_PROCESS_LOCATION" />
- </intent-filter>
- </receiver>
创建活动识别服务客户端
在Activity的OnCreate()方法中创建一个ActivityIdentificationService实例,通过该实例调用活动识别相关接口:
- private PendingIntent pendingIntent;
- private ActivityIdentificationService activityIdentificationService;
- protected void onCreate(Bundle savedInstanceState) {
- // 通过ActivityIdentification.getService()创建activityIdentificationService实例
- activityIdentificationService = ActivityIdentification.getService(this);
- // 获取PendingIntent对象
- pendingIntent = getPendingIntent();
- }
活动识别更新
使用活动识别服务,首先需要注册活动识别更新,可以检测用户当前是步行、骑自行车、静止等状态。
- 新建PendingIntent。
- // 获取自定义静态广播类LocationBroadcastReceiver关联的PendingIntent
- private PendingIntent getPendingIntent() {
- Intent intent = new Intent(this, LocationBroadcastReceiver.class);
- intent.setAction(LocationBroadcastReceiver.ACTION_PROCESS_LOCATION);
- return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- }
- 监听活动识别更新请求。
通过调用createActivityIdentificationUpdates(long detectionIntervalMillis, PendingIntent callbackIntent)方法,第一个参数为活动检测更新间隔(单位为毫秒),第二个参数pendingIntent。
- // 创建活动识别请求
- activityIdentificationService.createActivityIdentificationUpdates(5000, pendingIntent)
- // 请求成功监听回调
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Log.i(TAG, "createActivityIdentificationUpdates onSuccess");
- }
- })
- 10. // 请求失败监听回调
- 11. .addOnFailureListener(new OnFailureListener() {
- 12. @Override
- 13. public void onFailure(Exception e) {
- 14. Log.e(TAG, "createActivityIdentificationUpdates onFailure:" + e.getMessage());
- 15. }
- 16. });
- 移除活动识别更新。
在使用完活动识别后需要进行移除操作。调用deleteActivityIdentificationUpdates(PendingIntent pendingIntent)移除活动识别定时监听,参数PendingIntent必须与createActivityIdentificationUpdates(long detectionIntervalMillis, PendingIntent callbackIntent)参数里的PendingIntent是同一个。
- // 移除活动识别更新
- activityIdentificationService.deleteActivityIdentificationUpdates(pendingIntent)
- // 移除回调成功监听回调
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Log.i(TAG, "deleteActivityIdentificationUpdates onSuccess");
- }
- })
- 10. // 移除回调失败监听回调
- 11. .addOnFailureListener(new OnFailureListener() {
- 12. @Override
- 13. public void onFailure(Exception e) {
- 14. Log.e(TAG, "deleteActivityIdentificationUpdates onFailure:" + e.getMessage());
- 15. }
- 16. });
- 活动识别结果获取。
通过广播接收到的intent中获取活动识别结果。
- // 活动识别广播接收者
- public class LocationBroadcastReceiver extends BroadcastReceiver {
- // 活动识别服务广播action
- public static final String ACTION_PROCESS_LOCATION = "com.huawei.hms.location.ACTION_PROCESS_LOCATION";
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent != null) {
- final String action = intent.getAction();
- 10. if (ACTION_PROCESS_LOCATION.equals(action)) {
- 11. // 从活动识别服务发送的intent的extras中获取ActivityIdentificationResponse
- 12. ActivityIdentificationResponse activityIdentificationResponse = ActivityIdentificationResponse.getDataFromIntent(intent);
- 13. List<ActivityIdentificationData> list = activityIdentificationResponse.getActivityIdentificationDatas();
- 14. }
- 15. }
- 16. }
17. }
活动过渡更新
接口提供检测活动过渡条件(进入、退出)的功能,例如需要检测用户从走路变为骑自行车的状态时,应用通过调用createActivityConversionUpdates(ActivityConversionRequest request, PendingIntent pendingIntent)方法获取活动过渡的状态变化。
- 设置监听活动过渡请求参数。
- // 创建一个静止状态进入活动转换信息对象
- ActivityConversionInfo activityConversionInfoStillEnter = new ActivityConversionInfo(ActivityIdentificationData.STILL, ActivityConversionInfo.ENTER_ACTIVITY_CONVERSION);
- // 创建一个静止状态退出活动转换信息对象
- ActivityConversionInfo activityConversionInfoStillExit = new ActivityConversionInfo(ActivityIdentificationData.STILL, ActivityConversionInfo.EXIT_ACTIVITY_CONVERSION);
- List<ActivityConversionInfo> activityConversionInfos = new ArrayList<>();
- activityConversionInfos.add(activityConversionInfoStillEnter);
- activityConversionInfos.add(activityConversionInfoStillExit);
- // 创建一个活动转换请求体实例
- ActivityConversionRequest request = new ActivityConversionRequest();
10. request.setActivityConversions(activityConversionInfos);
- 监听活动过渡更新。
- // 监听活动识别状态转换
- Task<Void> task = activityIdentificationService.createActivityConversionUpdates(request, pendingIntent);
- // 任务成功监听回调
- task.addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Log.i(TAG, "createActivityConversionUpdates onSuccess");
- }
- })
- 10. // 任务失败监听回调
- 11. .addOnFailureListener(new OnFailureListener() {
- 12. @Override
- 13. public void onFailure(Exception e) {
- 14. Log.e(TAG, "createActivityConversionUpdates onFailure:" + e.getMessage());
- 15. }
- 16. });
- (可选)移除活动过渡更新。
不需要监听活动过渡条件时,需要调用deleteActivityConversionUpdates(PendingIntent pendingIntent)进行移除操作。
- // 通过指定pendingIntent移除活动转换更新
- activityIdentificationService.deleteActivityConversionUpdates(pendingIntent)
- // 移除更新成功监听回调
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Log.i(TAG, "deleteActivityConversionUpdates onSuccess");
- }
- })
- 10. // 移除更新失败监听回调
- 11. .addOnFailureListener(new OnFailureListener() {
- 12. @Override
- 13. public void onFailure(Exception e) {
- 14. Log.e(TAG, "deleteActivityConversionUpdates onFailure:" + e.getMessage());
- 15. }
- 16. });
- 返回结果获取。
活动过渡的结果:
- public class LocationBroadcastReceiver extends BroadcastReceiver {
- public static final String ACTION_PROCESS_LOCATION = "com.huawei.hms.location.ACTION_PROCESS_LOCATION";
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent != null) {
- final String action = intent.getAction();
- if (ACTION_PROCESS_LOCATION.equals(action)) {
- // 从intent中获取ActivityConversionResponse
- 10. ActivityConversionResponse activityConversionResponse = ActivityConversionResponse.getDataFromIntent(intent);
- 11. List<ActivityConversionData> list = activityConversionResponse.getActivityConversionDatas();
- 12. }
- 13. }
- 14. }
15. }
说明
海外版本手机活动识别不支持骑行和乘车。
活动识别API服务开发的更多相关文章
- 地理围栏API服务开发
地理围栏API服务开发 要使用华为地理围栏服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中. 指定应用权限 如果需要使用地理围栏服务 ...
- 某简单易懂的人脸识别 API 的开发环境搭建和简易教程
最近接了个人脸识别相关的项目,是基于某个非常简单易懂的人脸识别 API:face_recognition 做的.这个库接口非常傻瓜,很适合新手上手,而且可以研究其源码来学习 dlib 这个拥有更加灵活 ...
- 微信公众平台消息接口开发(24)图片识别之人脸识别API
微信公众平台开发模式 微信 公众平台 消息接口 开发模式 企业微信公众平台 图片识别 人脸识别 API 作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/archi ...
- 【.NET6】gRPC服务端和客户端开发案例,以及minimal API服务、gRPC服务和传统webapi服务的访问效率大对决
前言:随着.Net6的发布,Minimal API成了当下受人追捧的角儿.而这之前,程序之间通信效率的王者也许可以算得上是gRPC了.那么以下咱们先通过开发一个gRPC服务的教程,然后顺势而为,再接着 ...
- 免费人脸识别APi
今天对应一些免费的人脸识别的api 做了一下简单的对比,觉得百度开发出来的人脸识别接口还是最符合的我的要求,简单易用,容易上手. 据说百度的一些门禁也使用上了人脸识别的功能了,功能很强大,而且能识别出 ...
- 【新书推荐】《ASP.NET Core微服务实战:在云环境中开发、测试和部署跨平台服务》 带你走近微服务开发
<ASP.NET Core 微服务实战>译者序:https://blog.jijiechen.com/post/aspnetcore-microservices-preface-by-tr ...
- Google Map API V3开发(1)
Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...
- Google Map API V3开发(2)
Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...
- Google Map API V3开发(4)
Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...
随机推荐
- 【CPU100%排查】CPU100%问题排查方案
1.使用top -c 查看CPU 占用情况 ,按P(大写)可以倒序查看占CPU占用率 2.找到占用率高的进程以后,再定位到具体线程 比如 此时进程ID 14724 CPU占用高,进一步使用top - ...
- PAT 乙级 -- 1011 -- A+B和C
问题简述 给定区间[-231, 231]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1行给出正整数T(<=10),是测试用例的个数.随后给出T组测试用例,每组占一行,顺序 ...
- POJ2553 强连通出度为0的应用
题意: 给你一个有向图,然后问你有多少个满足要求的点,要求是: 这个点能走到的所有点都能走回这个点,找到所有的这样的点,然后排序输出. 思路: 可以直接一遍强连通缩点,所点之后 ...
- hdu2371 矩阵乘法(求序列位置改变m次后的序列)
题意: 给你一个字符串,然后让你执行m次操作,每次操作把当前的字符串映射到他给你的位置序列的位置,比如给的是 3 1 2,第一步就是把原来的3的位置的字母变到1的位置,1的变到2的位置,2 ...
- Nessus扫描器的使用
目录 Nessus Scans Settings 一个基本扫描的建立 自定义扫描策略 Nessus的高级扫描方法 Nessus Nessus号称是世界上最流行的漏洞扫描程序,全世界有超过75000个组 ...
- SMTP、POP3和IMAP邮件协议
目录 SMTP POP IMAP 总结 DNS记录中的MX记录 今天入职第一天,公司让配置个人的内网.外网邮箱,这可把我给搞晕了,本来以前就对邮箱这块不是很了解,平时也不怎么用邮箱,顶多有个QQ邮箱而 ...
- Windows Pe 第三章 PE头文件(下)
3.5 数据结构字段详解 3.5.1 PE头IMAGE_NT_HEADER的字段 1.IMAGE_NT_HEADER.Signature +0000h,双字.PE文件标识,被定义为00004550 ...
- 【Unity】实验二 游戏场景搭建
实验要求 实验二 游戏场景搭建 实验目的:掌握游戏场景搭建. 实验要求:能够使用Unity的地形引擎创建地形,熟悉场景中的光照与阴影,掌握天空盒和雾化效果等. 实验内容: 地形的绘制:使用高度图绘制: ...
- Redis(附Win10版本 和可视化工具)
启动服务端 通过win+r,cmd 运行命令行然后输入如下指令: G: cd software cd G:\software\redis-64.3.0.503 redis-server.exe 这样就 ...
- 多变量高斯(MVN)概率建模的两种方案
摘要:在我们的时序异常检测应用中,设计了对时序数据进行多变量高斯(MVN)建模的算法方案进行异常检测,本文对基于tensorflow的两种MVN建模方案进行了总结. 1.基于custom choles ...