B4A Library Physical Activity Recognition Detection

Status
Not open for further replies.
Class updated on May 2020.

This class allows you to monitor the user / device physical state (walking, running, still, etc.).

The state detection is based on Android location services which use low power sensors to try to detect the current activity. Once you connect your app to these services you will receive notifications even when your app is in the background (similar to static intent filters).

Configuration steps:
1. Add this line to the main activity:
B4X:
#AdditionalJar: com.google.android.gms:play-services-location
2. Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
AddPermission(com.google.android.gms.permission.ACTIVITY_RECOGNITION)
AddPermission(android.permission.ACTIVITY_RECOGNITION)

Recognition Service

Add a service named RecognitionService (must be this name) to your project. This service will receive the activity detection notifications.
The code should be similar to:
B4X:
Sub Process_Globals

End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    For Each ev As TransitionEvent In Starter.client.GetTransitionEvents(StartingIntent)
        Log(ev) 'ignore
        ToastMessageShow("" & ev, True)
    Next
    Service.StopAutomaticForeground
End Sub
The service will receive intents with the detection information. The intent includes two lists with the possible states and confidence levels. Both lists are sorted from the most probable activity to the least probably activity.


Listening for events

B4X:
'from an activity
Dim p As Phone
If p.SdkVersion >= 29 Then
    rp.CheckAndRequest("android.permission.ACTIVITY_RECOGNITION")
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission", True)
        Return
    End If
End If
Dim activities As List = Array("IN_VEHICLE", "ON_BICYCLE", "ON_FOOT", "STILL", "WALKING", "RUNNING")
Wait For (Starter.client.RequestTransitionUpdates(activities)) Complete (Success As Boolean)
Log(Success)


Notes

- Android may kill the process and then recreate it when a notification is delivered. This means that it is better to run the app in Release mode. Otherwise it will fail when the process is recreated.

History

V3.00 - Completely new version, released as a class instead of a library. It is based on a new Google API. Not backward compatible.
 

Attachments

  • ActivityRecognition.zip
    9.4 KB · Views: 277
Last edited:

brunnlechner

Member
Licensed User
Hallo,
the Activity Recognition Example works,
but when I press stop and then start any more activities are displayed.
Only when I finish in the task manager and start it works again.

This Example,
is an "improved" version of the Activity Recognition Example is works fine.
Activity Recognition Example Version 1.0 njdude.activityrecognition.sample
https://jumpshare.com/b/GLiDOG2y4aVjAigh1bz0

Thank you
Franz
 
Last edited:

brunnlechner

Member
Licensed User

Attachments

  • activity recognition sample.png
    activity recognition sample.png
    87.8 KB · Views: 147

Rusty

Well-Known Member
Licensed User
I'm having problems getting the Google-Play-service_lib\res installed.
When I bring up the SDK manager, it shows it is installed, but I keep getting this error:
ERROR: resource directory 'c:\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res' does not exist
The path exists up to here: c:\android-sdk\extras\google\google_play_services
And in fact, the file doesn't exist...
I've tried to install it, but no luck.
Ideas?
Thanks,
Rusty
 

Rusty

Well-Known Member
Licensed User
Thanks Don,
I upgraded to V6, the day it was released. No happiness on this one.
any other ideas?
Rusty
 

Rusty

Well-Known Member
Licensed User
Thanks Erel,
I'm surely doing something wrong.
I completely un-installed the Android SDK, downloaded a new one and installed it specifically selecting Google Play Services to be installed.
When I look at the C:\Android-SDK\extras\google\google_play_services folder, there is no .jar file at all.
There are only:
docs (folder)
samples (Folder)
source.properties (file)
Any advice on this?
Thanks,
Rusty
 

Rusty

Well-Known Member
Licensed User
...re removed and re installed SDK.
After MANY "install X packages", finally it installed required content as per your last post.
Thanks :)
 

Sgardy

Member
Licensed User
The attached sample uses this new library and displays the results on an activity, when the HOME key it pressed, the results will show on the status bar via a notification.

There's room for improvement, but this code might give you an idea.
I get this... why?

Compiling generated Java code. Error
B4A line: 45
ar.Initialize(\
javac 1.8.0_20
src\njdude\activityrecognition\sample\main.java:344: error: cannot access GooglePlayServicesClient
_v5.Initialize("ar",processBA);
^
class file for com.google.android.gms.common.GooglePlayServicesClient not found
 

MarcoRome

Expert
Licensed User
I get this... why?

Compiling generated Java code. Error
B4A line: 45
ar.Initialize(\
javac 1.8.0_20
src\njdude\activityrecognition\sample\main.java:344: error: cannot access GooglePlayServicesClient
_v5.Initialize("ar",processBA);
^
class file for com.google.android.gms.common.GooglePlayServicesClient not found
Look THIS
 

brunnlechner

Member
Licensed User
Button Start: Activity is displayed
Button Stop ( ar.Stop) : Activity is stopped
Button Start (ar.Connect(5000)): not Activity is displayed


https://www.b4x.com/android/forum/attachments/activityrecognition_sample-zip.

******************
Fixed bug: always ar.Initialize("ar") just before ar.Connect(5000)
******************
Button Start: Activity is displayed
Button Stop ( ar.Stop) : Activity is stopped
Button Start (ar.Connect(5000)): not Activity is displayed


https://www.b4x.com/android/forum/attachments/activityrecognition_sample-zip.
 

trueboss323

Active Member
Licensed User
Does this library work properly while you are driving but stopped at a red light? Would it stay at in vehicle or change to on foot?
 
Last edited:

Sgardy

Member
Licensed User
Hi Erel, why do I get this?

ar.Initialize(\
javac 1.8.0_20
src\b4a\example\main.java:344: error: cannot access GooglePlayServicesClient
_v5.Initialize("ar",processBA);
^
class file for com.google.android.gms.common.GooglePlayServicesClient not found
 
Status
Not open for further replies.
Top