Detect users’ behavior on Android (Kotlin) — Huawei Awareness Kit

Çağnur Hacımahmutoğlu Parçal
Huawei Developers
Published in
4 min readJun 8, 2021

Hello everyone,

While talking about how important it is to act in my previous article, I also mentioned that we sometimes forget to even take a step in our work from home process. At this point, I thought it would be very beneficial for us if we received a ‘take action’ notification according to our long-term inactivity at the time intervals we set and Awareness Kit’s Behavior feature, I developed an application that allows users to detect their current status and send them notifications at customized times. Actually, it would be more accurate to say that I have added a nice feature to our ‘healthy life’ application. :)

Before moving on to our development process, let’s take a look at what is the awareness kit and what are the advantages ?

What is Awareness Kit ?

Huawei Awareness Kit, provides your app with the ability to obtain contextual information including users’ current time, location, behavior, audio device status, ambient light, weather, and nearby beacons. Your app can gain insight into a user’s current situation more efficiently, making it possible to deliver a smarter, more considerate user experience.

What are the advantages ?

  • Converged: Multi-dimensional and evolvable awareness capabilities can be called in a unified manner.
  • Accurate: The synergy of hardware and software makes data acquisition more accurate and efficient.
  • Fast: On-chip processing of local service requests and nearby access of cloud services promise a faster service response.
  • Economical: Sharing awareness capabilities avoids separate interactions between apps and the device, reducing system resource consumption. Collaborating with the EMUI (or Magic UI) and Kirin chip, Awareness Kit can even achieve optimal performance with the lowest power consumption.

You can browse this page to examine it in more detail.

We learned about Huawei Awareness Kit and their advantages. Now we can move on development part. Primarily, we should create a project on App Gallery Connect. You can follow the steps from the link below.

Let’s start by adding the necessary permissions to our manifest file.

AndroidManifest

First of all, we call the behavior query API of the users by using the ‘capture client’ object of the Awareness Kit, and we start to create our process according to the returned result. In the profile section of the application, we offered users three options for how often they would like to receive notifications. These are 30 minutes, 1 hour and 2 hours. We wanted to increase the motivation of the users by sending a notification to the users that “you have not moved for a long time, take action for a healthy life”. While doing this, we made use of the Awareness Kit’s Behavior feature. The Behavior feature has its own methods and values ​​corresponding to these methods.

  • BEHAVIOR_IN_VEHİCLE ( value :0),
  • BEHAVIOR_ON_BICYCLE (value : 1),
  • BEHAVIOR_ON_FOOT (value :2),
  • BEHAVIOR_STILL (value : 3),
  • BEHAVI0R_UNKNOWN ( value :4),
  • BEHAVIOR_WALKING (value :7),
  • BEHAVIOR_RUNNING(value : 8)
Awareness Data

I used the ‘BEHAVIOR_STILL’ feature here. And I have detected the long-term inactivity of the users thanks to this feature. Here I defined PERIOD_COUNTER and NOTIFICATION_COUNTER. I look at the user’s inactivity status every 15 minutes and keep these values ​​accordingly. If the user does not select any time period, I set the period_counter default to 2 as I send a notification every 30 minutes by default. If the user is in the ‘still’ state during the period I mentioned above, I perform the control by increasing the notification_counter value, and in the last case, I compare the period_counter and notification_counter values.

callBehaviorAwareness

Finally, we define a Broadcast Receiver to listen to the operations.

AwarenessKitReceiver

We learned the Awareness Kit and used one of its features in the application. I hope it was useful for you too.

Inactivity notification

Thank you for reading. Your questions and opinions are very important to me.

See you in the next articles..

Done ! :)

References

--

--