Activity Recognition Client — Transition,Sampling and Sleep API

INTRODUCTION

In 2017 google has Introduced Activity Recognition Api to track the User Activities. But it has been Deprecated in 2020 and Google has Improved its Performance with Series of API’s, grouped into Activity Recognition Client.

HOW IT WILL WORK

Activity Recognition Client will Wake Up your device at Regular Intervals and then Collects the data from device’ s Sensors. Collected data will be used to determine the Activities with the Help of a Machine Learning algoritham

CONFIDENCE PERCENTAGE IN ARC

ARC Returns a List of Activities that User might doing with Some Confidence Percentage. This Confident Percentage tells you about the Surety of the Activity.

For Example, the activity which is having more than 75 Percent, then there is a Probablity that user Might doing the Activity

So the Confidence Percentage Tells you the probablity of an Activity which is done by User.

TYPES OF API

Activity Transition API — For Tracking Transistion Between API

Activity Sampling API — To Get Frequent Results for Subsequent Activity

Sleep API — For Detecting user Sleep Time

Activity Transition API

Types of Activity Transistion API

STILL : If User is Sitting in Same Place or Mobile Device has no Movement then ARC will Detect STILL activity

WALKING : If User is Walking with Device then ARC will Detect WALKING Activity

RUNNING : If User is Running or Jogging with Device then ARC will Detect RUNNING Activity

IN_VEHICLE : If User is Driving a Car or Bike with Device then ARC Detect IN_VEHICLE Activity

ON_BICYCLE : If User is Driving a Bicycle with Device then ARC Detect ON_BICYCLE Activity

Activity Sampling API

TILTING : Mobile Device is Lifted and is having some Angle with Flat Surface then ARC Detect TILTING Activity

ON_FOOT : ON_FOOT is Parent for WALKING and RUNNING and ARC will Detect this State when User is Moving on Normal Speed

UNKNOWN : If ARC Not able to Detect the User Activity then it will Return UNKNOWN state

Lets Start with an Example

DEPENDENCIES

Need to add a Play Service Location Dependency on build.gradle

PERMISSIONS

RUNTIME PERMISSION

If the Device Version is 29 or above we should Provide a runtime Permission for Activity Recognition Client

ADDING UI OF THE APPLICATION

In this Example I have 3 Buttons Start, Stop and Sleep with ImageView and Mutiple Textview for Confidence Level and Activity State.

Sample Code for Activity Transition and Sampling API

MainActivity.kt

Constants.java

BackgroundDetectedActivitiesService.kt

ActivityDetectionIntentService.kt

Declaring Services on Manifest

On the above Codes we can Achieve the Activity Transition and Sampling API Activities

SLEEP API

Android Sleep API will Collects the Information such as Surrounding Brightness and Device movment to make the Assumptions about when the User is asleep or awake.

This API is handy for tracking users’ sleep patterns to help them improve their sleep habits.

For Sleep API we have 2 Important Events Sleep Segment Event and Sleep Classify Event

SLEEP SEGMENT EVENT

Sleep Segment Event represents the Result of Segment Sleep after user is Awake. It Depends on the Status code below.

STATUS_MISSING_DATA — Sleep segment was detected, but there was some missing data near the detected sleep segment.

STATUS_NOT_DETECTED — Sleep segment is not detected in the past day, or there isn’t enough confidence that the user slept during the past day.

STATUS_SUCCESSFUL — Successfully detected sleep segment in the past day.

SLEEP CLASSIFY EVENT

Sleep Classify Event Includes

CONFIDENCE — Detect the Confidence Level which Ranges 0 to 100

MOVEMENT DETECTION — Detect the Movement Detection which Ranges From 1 to 6

LIGHT LEVEL — Detect the Light Level which Ranges From 1 to 6

Sample Code for SLEEP API

SleepReceiver.kt

SleepRequestManager.kt

Register Receiver in Manifest

SAMPLE SCREENSHOT

Walking Activity Recognition
Sleep Classify Event Sample

CONCLUSION

In above Sample we have learnt what is Activity Recognition Client and how it Works. Hope it will be Useful!!! Cheers!!!

Please refer to this Project Link : https://github.com/Hariharan1991/ActivityRecognizationAPI

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hariharan B

Hariharan B

Expertise on Android App Development and Progromming Languages Like Java, Kotlin, C# and TypeScript