Activity Identification Service | HMS Location Kit

Mustafa Sürücü
Huawei Developers
Published in
3 min readAug 12, 2020

Mobile applications need more information about their users to know and understand them much better. When Android developers get a bettter understanding of their users, they can offer life-changing services to humanity. One of these sensational information is Activity identification, in other words Activity Recognition.

In this post, we will examine Activity Identification service of HMS Location Kit to find out user motion status. If you have any questions about how you can integrate HMS Core into your project, please take a look at below post before beginning.

You should also activate HMS Location kit in your project. For details, please take a look at below post.

Activity identification service identifies user motion status through the acceleration sensor, cellular network information, and magnetometer, helping you adapt your app to user behavior. This service specifies activities that users can practise and can detect them by listening device motion. The activities can be recognized by the API are listed below.

  • VEHICLE : It can be detected while user is holding mobile device in a car, bus or any other vehicle.
  • BIKE : It can be detected when the mobile device is present on a moving bicycle.
  • FOOT : When user is walking or running, service can detect this activity.
  • STILL : When mobile device is motionless, service will detect this activity.
  • WALKING : When user is walking with mobile device, service will detect this activity.
  • RUNNING : When user is running with mobile device, service will detect this activity.
  • TILTING : When mobile device has an angle with a surface, service will detect this activity.
  • OTHER : When the device can not detect any activity, service will give this result.

Let’s discover the implementation with an example.

We should start by applying following permissions in the AndroidManifest.xml file to use Actitivity Identification service.

Since activity status updates identified by the activity identification service are broadcasted, we need to register a static broadcast receiver in the AndroidManifest.xml file.

Now, we will create ActivityIdentificationService instance in onCreate() method of the activity and use this instance to call activityUpdates request and remove methods. In addition, PendingIntent will be used to obtain identification results from BroadcastReceiver.

requestActivityUpdates() and removeActivityUpdates() methods have been called as you can see above code block. These methods have been created inside ActivityIdentificationActivity class with activityIdentificationService and pendingIntent parameters.

In requestActivityUpdates() method, an identification listener is added to the broadcast receiver and createActivityIdentificationUpdates method has been triggered with activityIdentificationService object.

In removeActivityUpdates() method, identification listener is removed from broadcast receiver and deleteActivityIdentificationUpdates method has been triggered with activityIdentificationService object.

Note : detectionIntervalMillis should pass as a parameter of the request method. It defines period of activity update.

The last part is creating LocationBroadcastReceiver class where we will obtain activity identification results by using PendingIntent.

ActivityIdentificationResponse class have been used to fetch data from intent inside onReceive. After gathering activity results in a list, identified activities are assigned to integer variable which is defined as “type”. After that, as an example of identification we will show Toast messages when any activity has been identified by the service.

User Activity in Demo — 1
User Activity in Demo — 2

In this article, we handled the subject of Activity Identification with HMS Location Kit. I hope that it can shed light on your stunning ideas that you will use in your applications.

Thank you for reading !

References:

--

--