Intermediate: Huawei Activity Identification Service | HMS location kit

RChancha
4 min readMay 10, 2021

Introduction

Nowadays, everybody is using smartphones to do daily tasks like taking photos, looking up movie times, making calls etc. The best part of Android apps on mobile phones is that they are trying more and more to get to know their users. Many applications today take users’ locations to provide users with locational feeds. One common example is a normal news app, where the app takes your current location and shows the news by location.

If you’re a developer, you need to understand users better to give users a better experience of the application. You should know at any time what your users do. The more you know about your users, the better application for your users can build. For example, a distance calculator app lunches by itself when you start driving yourcar or bike and stops when you stop driving. Health and fitness app also uses this service to determine how many meters/kilometers you have covered on particular day.

What is Activity Identification Service?

Activity Identification Service does the heavy lifting using acceleration sensor, cellular network information and magnetometer from device to identify user’s current activity. Your app receives a list of detected activities, each of which includes possibility and identity properties.

The Activity Identification Service can detect following activities:

  • STILL: When the mobile device will be still, that is, the user is either sitting at someplace or the mobile device is having no motion, then the Activity Recognition Client will detect the STILL activity.
  • FOOT: When the mobile device is moving at a normal speed , that is, the user carrying the mobile device is either walking or running then the Activity Identification Service will detect the FOOT activity.
  • WALKING: This is a sub-activity of the FOOT activity which is detected by the Activity Identification Service when the user carrying the mobile device is walking.
  • RUNNING: This is also a sub-activity of FOOT activity which is detected by the Activity Recognition Client when the user carrying the mobile device is running.
  • VEHICLE: This activity detected when the mobile device is on the bus or car or some other kind of vehicle or the user holding the mobile device is present in the vehicle.
  • OTHERS: The Activity Identification service will show this result when the device is unable to detect any activity on the mobile device.

In this article, we will create a sample application to show user activity. When user clicks start button, we will identify user activity status along with possibility level and display the status in Textview and Imageview. And when user clicks on stop button, we will stop requesting activity identification updates.

Development Overview

Prerequisite

1. Must have a Huawei Developer Account.

2. Must have Android Studio 3.0 or later.

3. Must have Huawei phone running EMUI 5.0 or later.

4. EMUI 5.0 or later.

Software Requirements

1. Java SDK 1.7 or later.

2. Android 5.0 or later.

Preparation

1. Create an app or project in the Huawei App Gallery Connect.

2. Provide the SHA Key and App Package name of the project in App Information Section and enable the Location Kit API.

3. Download the agconnect-services.json file.

4. Create an Android project.

Integration

  1. Add below to build.gradle (project) file under buildscript/repositories and allprojects/repositories.

2. Add below to build.gradle (app) file, under dependencies to use the Location kit SDK.

Tip: Minimum android version supported for these kits is 19.

3. Add below permissions to manifest file.

For version earlier than android Q

<uses-permission android:name=”com.huawei.hms.permission.ACTIVITY_RECOGNITION”/>

For version Android Q and later

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

Note: The above permissions are dangerous permission and need to be requested dynamically. Requesting permission dynamically is not covered in this article.

Development

We need to register static broadcast receiver in AndroidManifest.xml to listen to activity status update identified by Activity Identification Service.

Now the next step is to add the UI for our Main Activity. In our application, we will be having one TextView to display the name of the current activity and display corresponding image on ImageView and one TextView to display the possibility of Activity. We will have two Buttons to start and stop activity identification tracking. So, the activity_main.xml file looks something like this:

Now let’s create instance of ActivityIdentificationService in onCreate() method of MainActivity.java

To obtain PendingIntent object

When user clicks on Start Tracking Button, we will request activity identification updates by calling createActivityIdentificationUpdates() method.

This method has two parameters: detectionIntervalMillis and pendingIntent, which indicate the detection interval (in milliseconds) and action to perform, respectively.

On click of Stop Tracking Button, we will stop activity identification updates.

Finally, We can get activity identification result (containing identity and possibility) from intent received by the broadcast receiver.

getActivityIdentificationDatas() API is used to obtain the list of activitiy identification list. The activity identifications are sorted by most probable activity first.

We have created Utils.java class to obtain activity status from identity code obtained from LocationReceiver

Code snippet of MainActivity.java

Tips and Tricks
1.During writing of this article, the activity identification service cannot identify the cycling and riding activities on devices outside the Chinese mainland.
2. ACTIVITY_RECOGNITION is dangerous permission and should be requested dynamically.

Conclusion
In this article, we have learnt how to use the Activity Identification Service in our application to determine the activities that users are doing at any given time. The Activity Identification Service determines the ongoing activities based on a possibility value that tells you which activity is currently taking place.
Hope you found this story useful and interesting.
Happy coding! 😃 💻

References
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/introduction-0000001050706106-V5

--

--