{{announcement.body}}
{{announcement.title}}

Behavior Awareness : A Journey Through HMS Awareness Part 2

DZone 's Guide to

Behavior Awareness : A Journey Through HMS Awareness Part 2

Awareness Ki provides your app with the ability to obtain contextual information, including users' current time, location, behavior, etc.

· Web Dev Zone ·
Free Resource

Introduction

Behavior Awareness is being  used to obtain user current behaviour or detect the behavior change.

For calling Behavior Awareness capability we have to assign the given permissions in the manifest file.

HTML


Capture API

We can use the  Capture  API  to detect user behavior, such as walking, running, cycling, driving, etc.

To get current behavior status of a user, we need to call the getBehavior() method - this will return an instance of the BehaviorResponse class that if successful, will contain information about the current user behavior.

Java


Barrier API

We can use the  Barrier API to detect the behavior change such as from walking  to running. 

Constant Summary

The below table gives the deatils of different types of behavior.

Name

Constant Value

Description

BEHAVIOR_IN_VEHICLE

0

The user is driving

BEHAVIOR_ON_BICYCLE

1

The user is cycling

BEHAVIOR_ON_FOOT

2

The user is walking or running

BEHAVIOR_STILL

3

The user is still

BEHAVIOR_WALKING

7

The user is walking.

BEHAVIOR_RUNNING

8

The user is running.

BEHAVIOR_UNKNOWN

4

Indicates that the current status of the user cannot be detected.

Note: Unknown behavior barriers cannot 


Public Methods

Behavior barriers, including the beginning, ending, and keeping barriers of behaviors. Given three mathods which we use in behavior barrier.

public static AwarenessBarrier beginning(int… behaviorTypes)

When a user is in the behavior status, the barrier status is TRUE and a barrier event is reported. After 5s, the barrier status changes to FALSE.

Java


public static AwarenessBarrier keeping(int… behaviorTypes)

When a user is in the behavior status , the barrier status is TRUE and a barrier event is reported.

Java


public static AwarenessBarrier ending(int… behaviorTypes)

When a user stops the behavior, the barrier status is TRUE and a barrier event is reported. After 5s, the barrier status changes to FALSE.

Java


Sample Code

Given an example in which a barrier is triggered by the keeping condition. That is, the barrier will be triggered when a user is in stand still.

Java


References

https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/awareness-behavior-dev

Topics:
android, integration, java, mobile, opensource

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}