活动识别API服务开发

活动识别API服务开发

要使用华为活动识别服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中。

指定应用权限

  • 在Android Q以下版本使用活动识别需要在“AndroidManifest.xml”文件中配置以下权限:
  1. <uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION"/>
  • 在Android Q及以上版本中,需要在“AndroidManifest.xml”文件中申请以下权限:

 .          <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

说明

以上活动识别相关权限属于危险权限,使用时需要动态申请。

注册静态广播

示例代码中活动识别服务的活动状态更新信息需要广播接收,因此需要在“AndroidManifest.xml”文件中注册广播接收器。

  1. <!--注册活动识别服务广播接收器-->
  2. <receiver
  3.     android:name=".location.fusedlocation.LocationBroadcastReceiver"
  4.     android:exported="true">
  5.     <intent-filter>
  6.         <action android:name="com.huawei.hmssample.location.LocationBroadcastReceiver.ACTION_PROCESS_LOCATION" />
  7.     </intent-filter>
  8. </receiver>

创建活动识别服务客户端

在Activity的OnCreate()方法中创建一个ActivityIdentificationService实例,通过该实例调用活动识别相关接口:

  1. private PendingIntent pendingIntent;
  2. private ActivityIdentificationService activityIdentificationService;
  3. protected void onCreate(Bundle savedInstanceState) {
  4.     // 通过ActivityIdentification.getService()创建activityIdentificationService实例
  5.     activityIdentificationService = ActivityIdentification.getService(this);
  6.     // 获取PendingIntent对象
  7.     pendingIntent = getPendingIntent();
  8. }

活动识别更新

使用活动识别服务,首先需要注册活动识别更新,可以检测用户当前是步行、骑自行车、静止等状态。

  1. 新建PendingIntent。
  2. // 获取自定义静态广播类LocationBroadcastReceiver关联的PendingIntent
  3. private PendingIntent getPendingIntent() {
  4.     Intent intent = new Intent(this, LocationBroadcastReceiver.class);
  5.     intent.setAction(LocationBroadcastReceiver.ACTION_PROCESS_LOCATION);
  6.     return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  7. }
  8. 监听活动识别更新请求。

通过调用createActivityIdentificationUpdates(long detectionIntervalMillis, PendingIntent callbackIntent)方法,第一个参数为活动检测更新间隔(单位为毫秒),第二个参数pendingIntent。

  1. // 创建活动识别请求
  2. activityIdentificationService.createActivityIdentificationUpdates(5000, pendingIntent)
  3.     // 请求成功监听回调
  4.     .addOnSuccessListener(new OnSuccessListener<Void>() {
  5.         @Override
  6.         public void onSuccess(Void aVoid) {
  7.             Log.i(TAG, "createActivityIdentificationUpdates onSuccess");
  8.         }
  9.     })
  10. 10.     // 请求失败监听回调
  11. 11.     .addOnFailureListener(new OnFailureListener() {
  12. 12.         @Override
  13. 13.         public void onFailure(Exception e) {
  14. 14.             Log.e(TAG, "createActivityIdentificationUpdates onFailure:" + e.getMessage());
  15. 15.         }
  16. 16.     });
  17. 移除活动识别更新。

在使用完活动识别后需要进行移除操作。调用deleteActivityIdentificationUpdates(PendingIntent pendingIntent)移除活动识别定时监听,参数PendingIntent必须与createActivityIdentificationUpdates(long detectionIntervalMillis, PendingIntent callbackIntent)参数里的PendingIntent是同一个。

  1. // 移除活动识别更新
  2. activityIdentificationService.deleteActivityIdentificationUpdates(pendingIntent)
  3.     // 移除回调成功监听回调
  4.     .addOnSuccessListener(new OnSuccessListener<Void>() {
  5.         @Override
  6.         public void onSuccess(Void aVoid) {
  7.             Log.i(TAG, "deleteActivityIdentificationUpdates onSuccess");
  8.         }
  9.     })
  10. 10.     // 移除回调失败监听回调
  11. 11.     .addOnFailureListener(new OnFailureListener() {
  12. 12.         @Override
  13. 13.         public void onFailure(Exception e) {
  14. 14.             Log.e(TAG, "deleteActivityIdentificationUpdates onFailure:" + e.getMessage());
  15. 15.         }
  16. 16.     });
  17. 活动识别结果获取。

通过广播接收到的intent中获取活动识别结果。

  1. // 活动识别广播接收者
  2. public class LocationBroadcastReceiver extends BroadcastReceiver {
  3.     // 活动识别服务广播action
  4.     public static final String ACTION_PROCESS_LOCATION = "com.huawei.hms.location.ACTION_PROCESS_LOCATION";
  5.  
  6.     @Override
  7.     public void onReceive(Context context, Intent intent) {
  8.         if (intent != null) {
  9.             final String action = intent.getAction();
  10. 10.             if (ACTION_PROCESS_LOCATION.equals(action)) {
  11. 11.                  // 从活动识别服务发送的intent的extras中获取ActivityIdentificationResponse
  12. 12.                  ActivityIdentificationResponse activityIdentificationResponse = ActivityIdentificationResponse.getDataFromIntent(intent);
  13. 13.                  List<ActivityIdentificationData> list = activityIdentificationResponse.getActivityIdentificationDatas();
  14. 14.             }
  15. 15.         }
  16. 16.     }

17. }

活动过渡更新

接口提供检测活动过渡条件(进入、退出)的功能,例如需要检测用户从走路变为骑自行车的状态时,应用通过调用createActivityConversionUpdates(ActivityConversionRequest request, PendingIntent pendingIntent)方法获取活动过渡的状态变化。

  1. 设置监听活动过渡请求参数。
  2. // 创建一个静止状态进入活动转换信息对象
  3. ActivityConversionInfo activityConversionInfoStillEnter = new ActivityConversionInfo(ActivityIdentificationData.STILL, ActivityConversionInfo.ENTER_ACTIVITY_CONVERSION);
  4. // 创建一个静止状态退出活动转换信息对象
  5. ActivityConversionInfo activityConversionInfoStillExit = new ActivityConversionInfo(ActivityIdentificationData.STILL, ActivityConversionInfo.EXIT_ACTIVITY_CONVERSION);
  6. List<ActivityConversionInfo> activityConversionInfos = new ArrayList<>();
  7. activityConversionInfos.add(activityConversionInfoStillEnter);
  8. activityConversionInfos.add(activityConversionInfoStillExit);
  9. // 创建一个活动转换请求体实例
  10. ActivityConversionRequest request = new ActivityConversionRequest();

10. request.setActivityConversions(activityConversionInfos);

  1. 监听活动过渡更新。
  2. // 监听活动识别状态转换
  3. Task<Void> task = activityIdentificationService.createActivityConversionUpdates(request, pendingIntent);
  4. // 任务成功监听回调
  5. task.addOnSuccessListener(new OnSuccessListener<Void>() {
  6.         @Override
  7.         public void onSuccess(Void aVoid) {
  8.             Log.i(TAG, "createActivityConversionUpdates onSuccess");
  9.         }
  10.     })
  11. 10.     // 任务失败监听回调
  12. 11.     .addOnFailureListener(new OnFailureListener() {
  13. 12.         @Override
  14. 13.         public void onFailure(Exception e) {
  15. 14.             Log.e(TAG, "createActivityConversionUpdates onFailure:" + e.getMessage());
  16. 15.         }
  17. 16.     });
  18. (可选)移除活动过渡更新。

不需要监听活动过渡条件时,需要调用deleteActivityConversionUpdates(PendingIntent pendingIntent)进行移除操作。

  1. // 通过指定pendingIntent移除活动转换更新
  2. activityIdentificationService.deleteActivityConversionUpdates(pendingIntent)
  3.     // 移除更新成功监听回调
  4.     .addOnSuccessListener(new OnSuccessListener<Void>() {
  5.         @Override
  6.         public void onSuccess(Void aVoid) {
  7.             Log.i(TAG, "deleteActivityConversionUpdates onSuccess");
  8.         }
  9.     })
  10. 10.     // 移除更新失败监听回调
  11. 11.     .addOnFailureListener(new OnFailureListener() {
  12. 12.         @Override
  13. 13.         public void onFailure(Exception e) {
  14. 14.             Log.e(TAG, "deleteActivityConversionUpdates onFailure:" + e.getMessage());
  15. 15.         }
  16. 16.     });
  17. 返回结果获取。

活动过渡的结果:

  1. public class LocationBroadcastReceiver extends BroadcastReceiver {
  2.     public static final String ACTION_PROCESS_LOCATION = "com.huawei.hms.location.ACTION_PROCESS_LOCATION";
  3.  
  4.     @Override
  5.     public void onReceive(Context context, Intent intent) {
  6.         if (intent != null) {
  7.             final String action = intent.getAction();
  8.             if (ACTION_PROCESS_LOCATION.equals(action)) {
  9.                 // 从intent中获取ActivityConversionResponse
  10. 10.                 ActivityConversionResponse activityConversionResponse = ActivityConversionResponse.getDataFromIntent(intent);
  11. 11.                 List<ActivityConversionData> list = activityConversionResponse.getActivityConversionDatas();
  12. 12.             }
  13. 13.         }
  14. 14.     }

15. }

说明

海外版本手机活动识别不支持骑行和乘车。

 

posted @ 2021-01-29 07:18  吴建明wujianming  阅读(213)  评论(0编辑  收藏  举报